Search notes:

Python: using the global statement in modules

The following example tries to demonstrate the usage of the global statement in a module.

The module (tq84_mod.py)

The module has one function, func(), which assigns a value (assigned in module's func()) to a global variable. This variable is global within the module, not within the entire execution.
def func():
    global var
    var = "assigned in module's func()"
Github repository about-python, path: /statements/global/module/tq84_mod.py

The script

The script imports the module, assigns a value to var calls func() in the module and then prints the module's and the script's value of var.
def func():
    global var
    var = "assigned in module's func()"
Github repository about-python, path: /statements/global/module/tq84_mod.py
Because the script's (global) var variable is another variable than the module's global var variable, the script, when executed, prints:
var          = assigned in script
tq84_mod.var = assigned in module's func()

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199199, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/statements/global/module(60): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78