the double underscore mangles the class name with the attribute, prefixed by a single underscore
c._C1__pri
allows access to the attribute. Sort of a pseudo-privacy, but it has other uses.
I'm not sure what you expected to happen with c.__private
. I think you meant to do c.__pri
though the result would have been the same.
the __pri
attribute is not private because it can be accessed outside of the class, it's just a few characters extra to do so.
as jadkik94 mentioned below, this isn't to make it impossible to access, but to signal to another programmer that they shouldn't be using the attribute.
FYI another time this may come in handy is when using inheritance
class Parent:
def __method(self):
print 'Parent method'
class Child(Parent):
def __method(self):
print 'Child method'
>>> c = Child()
>>> c._Child__method()
Child method
>>> c._Parent__method()
Parent method
the double underscore prevents accidental overriding of the name (not that this is seen very often).