具体区别看下面例子
class A:
def __init__(self,name='Andy'):
self._name = name class B:
def __init__(self,name = 'Jack',age=19):
self.__name = name # 私有属性(变量),只有类对象自己能访问,子类也不能访问
self._age = age # 保护变量,类,及子类对象可以访问 class C(B):
def h(self):
print('hello') a = A()
b = B()
c = C()
print(a._name) # 输出Adny
print(b._B__name) # 私有属性在类外的访问方式:对象._类__属性名
print(c._age) # 输出19
print(c._C__name) # AttributeError: 'C' object has no attribute '_C__name'子类不能访问