self.__foo = foo,雙下劃線為私有屬性
# 類的方法和屬性的訪問權限
class Test:
def __init__(self, foo):
self.__foo = foo
def __bar(self):
print(self.__foo)
print('__bar')
# __bar,__foo無法在外部訪問
def main():
test = Test('hello')
test.__bar() # AttributeError: 'Test' object has no attribute '__bar'
print(test.__foo) # AttributeError: 'Test' object has no attribute '__foo'
if __name__ == '__main__':
main()