variable scope
Carl Banks
pavlovevidence at gmail.com
Tue Sep 29 10:22:54 EDT 2009
On Sep 29, 3:11 am, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
> Mark Dickinson a écrit :
>
>
>
> > On Sep 28, 9:37 am, Bruno Desthuilliers <bruno.
> > 42.desthuilli... at websiteburo.invalid> wrote:
> >> Joel Juvenal Rivera Rivera a écrit :
>
> >>> 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.
> >> (snip)
> >>>> 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
> >> Looks like a bug to me. I Think you should fill a ticket...
>
> > I don't think it's a bug. Unless I'm missing something,
> > it's the 'names in class scope are not accessible' gotcha,
>
> I would of course expect the class level name "__a" to be unreachable
> from within __init__. What puzzle me is that local name "__a" (*not*
> self.__a) in the __init__ resolves to the module level name "_uno__a".
>
> > The double underscores and name mangling are a red herring:
>
> I beg to disagree. The problem (well... what I think is a problem,
> actually) IS that name mangling is applied to a method *local* variable.
It's not (__a is a global), and I'll opine that the behavior is more
consistent and more easily explained the way it is. Consider the
following:
import foo
class uno(object):
def __init__(self):
__bar = 1
foo.__bar = 1
Now you have to explain why __bar would be name-mangled when set in
the foo module, but not when set in the current module.
Carl Banks
More information about the Python-list
mailing list