variable scope

Ethan Furman ethan at stoneleaf.us
Fri Sep 25 18:14:12 EDT 2009


Joel Juvenal Rivera Rivera wrote:
> Hi i was playing around with my code the i realize of this
> 
> ###################
> _uno__a = 1
> class uno():
>     __a = 2
>     def __init__(self):
>         print __a
> uno()
> ###################
> and prints 1
> 
> So when i create class uno in the __init__ calls the global _uno__a when
> i refer just __a ? it's some kind of "private global" variable?
> 
> Regards
> 
> Joel Rivera
> 


Wow, that's interesting.  Looks like you have simultaneously kicked in 
name mangling[1], while not using the 'self' notation to specify an 
instance variable and not a global variable.

For an instance variable you should use self.__a, not just __a.  And you 
don't want to use two leading underscores until you know what you're 
doing.  :-)

[1] http://www.python.org/doc/1.5/tut/node67.html
     http://docs.python.org/reference/expressions.html
          in 5.2.1 Identifiers


Hope this helps!

~Ethan~



More information about the Python-list mailing list