Python double underscore mangling

Posted by gnr on Stack Overflow See other posts from Stack Overflow or by gnr
Published on 2012-11-21T16:52:49Z Indexed on 2012/11/21 16:59 UTC
Read the original article Hit count: 354

I am a bit confused by this behavior (using python 3.2):

class Bar: pass
class Foo:
    def __init__(self):
        self.__cache = None

bar = Bar()
bar.__cache = None

foo = Foo()

print(vars(bar))        #returns {'__cache': None}
print(vars(foo))        #returns {'_Foo__cache': None}

I've read up a bit on how double-underscores cause attribute names to be "mangled", but I would have expected the same name-mangling in both cases above.

The meaning of a single- and a double-underscore before an object name in Python

Any ideas what's going on here?

© Stack Overflow or respective owner

Related posts about python

Related posts about attributes