1、在python中可以在变量和函数名前加上双下划线—‘’__‘’来实现其伪私有(实际上python中没有Private属性的),加上双下划线后,外部对象不能通过调用其名称直接获得对象的属性或操作。

>>> class Person:
    __name = '小甲鱼'

    
>>> p = Person()
>>> p.name
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    p.name
AttributeError: 'Person' object has no attribute 'name'

    以上代码展示了name属性的伪封装。

  1、如果想访问name属性,可以通过以下方法:通过设置getName()方法获得name属性。

            

  2、双下划线的本质。在变量名前加 ''__'' ,python 实际上是修改了变量名,将被修饰的变量名改成了 "_类名__变量名" 的形式,也就是所谓的name mangling,可以直接通过  对象._类名__变量名的形式调用。如下截图: