From an object-oriented perspective on the Python language, there is no language feature that enforces private access to class members (hides them from outside the class). By convention, one or two underscores (_
) at the beginning of a member name indicate that it is private / must not be accessed from the outside, but prohibit those access altogether. Can't.
"We are all (consenting) adults here" --Guido van Rossum (Python language author) or the Python community
class Person:
#Private function (_Is one)
def _privateFun(self):
print("private!")
#Private function (_2)
def __morePrivateFun(self):
print("secret!")
target = Person()
target._privateFun() # OK
target.__morePrivateFun() #This is NG
target._Person__morePrivateFun() # OK
Recommended Posts