PyWart: Namespace asinitiy and the folly of the global statement

Michael Torrie torriem at gmail.com
Fri Feb 8 01:21:35 EST 2013


On 02/07/2013 09:30 PM, Rick Johnson wrote:
> count = 0
> class Blah:
>     def meth():
>         for x in range(100):
>             count = x
>      
> Where is count living?
> 
> Of course in this simplistic example we can see that count is @
> module level

Except that it's not after the "count=x" statement inside the for loop.
 That's entirely within the local scope.

Names bound to objects always default to the local namespace, whatever
that is.  If your function referred to "count" without any assignment,
then it's unclear as to which namespace it is in.  It could be the class
namespace, or the module namespace.  But that's a problem no different
than in any language.

Python does differ, from, say C, where global variables can be read and
written to without any special declarations in a function, though you
can tell at a glance whether or not a variable is declared in the local
scope.




More information about the Python-list mailing list