newbie-question: Nested functions and variable scope

Emile van Sebille emile at fenx.com
Thu Sep 5 00:59:04 EDT 2002


Leonardo:
> If I put "in_reply" outside everything and use "global in_reply"
> inside each nested function, it works, but that's evil! What would be
> a better way of doing it? Do I need to use a class?

Take a look at nested_scopes:

ActivePython 2.1, build 210 ActiveState)
based on Python 2.1 (#15, Apr 19 2001, 10:28:27) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def level1():
...     ir = 1
...     def lvl2b():
...             print ir
...
<stdin>:1: SyntaxWarning: local name 'ir' in 'level1' shadows use of
'ir' as glo
bal in nested scope 'lvl2b'
>>> from __future__ import nested_scopes
>>> def level1():
...     ir = 1
...     def lvl2b():
...             print ir
...


HTH,

--

Emile van Sebille
emile at fenx.com

---------







More information about the Python-list mailing list