class Foo: def __func(self): #私有属性在定义阶段就发生变化,_Foo__func print(‘Foo‘) class Bar(Foo): def __func(self): #因为在定义阶段就发生了变化,所以子类不能继承父类的私有属性 print(‘Bar‘) b = Bar() b._Foo__func() b._Bar__func() class A: def foo(self): print(‘A.foo‘) self.__bar() #调用私有方法,找不到,除了自己类以外的方法 def __bar(self): print(‘A.bar‘) class B(A): def __bar(self): print(‘B.bar‘) b = B() b.foo() class Teacher(): def __init__(self,username,password): if not isinstance(password,str): raise Exception(‘密码只能是字符串‘) self.__uname = username self.__pwd = password @property def name(self): return self.__uname @name.setter def name(self,val): print(type(val)) if not isinstance(val,str): raise TypeError(‘must be str‘) self.__uname=val @name.deleter def name(self): import re if re.findall(‘(?i)sb‘,self.__uname): #(?i) 不区分大小写 raise PermissionError(‘SB not Deleting permissions‘) else: del self.__uname egon = Teacher(‘egon‘,‘123‘) egon.name = (‘egonSB‘) print(egon.name) del egon.name