用兩個底線開頭的函數是私人函數. 雖然說是私人的, 但是還是可以在類外引用
>>> class test:<br />def __private():<br />print("private");</p><p>>>> a = test()<br />>>> a.__private()<br />Traceback (most recent call last):<br /> File "<pyshell#5>", line 1, in <module><br /> a.__private()<br />AttributeError: 'test' object has no attribute '__private'<br />>>> a._test__private()<br />Traceback (most recent call last):<br /> File "<pyshell#6>", line 1, in <module><br /> a._test__private()<br />TypeError: __private() takes no arguments (1 given)<br />>>> dir(test)<br />['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_test__private']<br />>>> test._test__private()<br />private<br />>>>