Search notes:

Python Exception: UnboundLocalError: local variable '…' referenced before assignment

The following simple script causes the Exception UnboundLocalError to be raised.
This is because Python assumes a variable to be local to a Python functions if this variable is being assigned to in that function.
a_global_var = 42

def increment_global_var():
    a_global_var += 1

increment_global_var()
Github repository about-Python, path: /statements/global/local-variable-referenced-before-assignment.py
In order to be able to assign a value to a global variable in a function, it must be declared as global using the global statement.

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...', 1758199876, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/statements/global/UnboundLocalError-local-variable-referenced-before-assignment(47): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78