variable scope

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Sep 29 12:11:14 CEST 2009


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.





More information about the Python-list mailing list