The purpose of name mangling is to prevent accidental name collisions from defining the same attribute in a subclass. Note that this has nothing to do with preventing access to the attribute. If you want to pretend that language level access control is a meaningful security boundary, go use some other language. Python's motto is that "we're all consenting adults".
Anyway, your example does work once you correct the syntax.
class A(object):
__a = 32
def func(self):
return self.__a
class B(A):
__a = 41
def bar(self):
return self.__a
def bar2(self):
return self._A__a
print A().func()
print B().func()
print B().bar()
print B().bar2()
B().func()
still prints out 32, because it is accessing the version of __a
defined in class A
.