variable scope

Joel Juvenal Rivera Rivera joelriv at gmail.com
Sat Sep 26 01:00:49 CEST 2009


Yeah i forgot the self an try the code then i see
an error that it was not defines _uno__a so that's
where i define the global and see that behavior.

Thanks for your answers

El vie, 25-09-2009 a las 15:14 -0700, Ethan Furman escribió:
> 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