Newcomer question wrt variable scope/namespaces

Florian Daniel Otel florian.otel at gmail.com
Fri Jan 13 04:18:27 EST 2006


Hello all,

As the subject says, I am a newcomer to Python and I have a newcomer
question wrt namespaces and variable scope. Obviously, I might be
missing smth obvious, so TIA for the patience and/or pointers to
relevant resources

My problem: I just discovered (by mistake) that attempting to assign a
value to a non-local dictionary/list member does NOT generate an "
UnboundLocalError" exception and the assignment is preserved upon
exiting that scope (i.e. function). This would "violate" the python
scoping rules where a variable in a global scope can only be
referenced to (and not assigned to)  -- unless declared as "global".

Attached are 3 small python scripts that illustrate my "problem". The
first one uses string tuples and behaves as expected. The other two
use dictionaries and (resp.) lists and illustrate my "problem"

TIA for any pointers,

Florian

P.S. I am not reading the newsgroup / subscribed to the mailing list,
so please Cc: me on the replys.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 001-scope.py
Type: application/x-python
Size: 752 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060113/49d1b0f5/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 002-scope.py
Type: application/x-python
Size: 442 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060113/49d1b0f5/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 003-scope.py
Type: application/x-python
Size: 399 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060113/49d1b0f5/attachment-0002.bin>


More information about the Python-list mailing list