Two attributes! Why?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Nov 29 00:21:30 EST 2008
On Fri, 28 Nov 2008 19:22:42 -0800, Emanuele D'Arrigo wrote:
> Hi everybody,
>
> I'm having a naming/scoping/mangling issue. I have a class like this:
>
> class MyClass(object):
> __init__(self):
> self.__myAttribute = None
>
> def myMethod(self, aValue):
> attributeName = "__myAttribute"
> setattr(self, attributeName, aValue)
>
> It all looks good to me but when I use a debugger I find that
>
> - myClass._MyClass__myAttribute = None
>
> and
>
> - myClass.__myAttribute = aValue
>
> I can't quite understand why.
This is a side-effect of name-mangling. Double-underscore names are only
mangled when they are referred to directly as attributes, not when they
are passed to setattr, getattr etc. Those functions don't do any name
mangling.
--
Steven
More information about the Python-list
mailing list