Python类中的 私有变量和私有方法print a.__data #外界不能访问私有变量 AttributeError: ‘A’ object has no attribute ‘__data’print a.__name #访问私有属性,报错!AttributeError: A instance has no attribute ‘__name’a.__say()#调用私有方法,报错。AttributeError: A instance has no attribute ‘__say’ins
转载来自https://blog.csdn.net/sxingming/article/details/52875125 默认情况下,Python中的成员函数和成员变量都是公开的(public),在python中没有类似public,private等关键词来修饰成员函数和成员变量。 在python中定义私有变量只需要在变量名或函数名前加上 ”__“两个下划线,那么这个函数或变量就是私有的了。 ...