1.property装饰器
property装饰器就是把方法伪装成属性给对象调用
eg
class Student():
__school = 'SH' # _Student__school => _类名__属性名
def __init__(self, name, age):
self.__name = name
self.age = age
def __tell_info(self): # _Student__tell_info => _类名__函数名
print('name: %s, age: %s' % (self.__name, self.age))
@property # 把方法伪装成属性
def name(self):
return "name:%s" % self.__name
@name.setter
def name(self, v):
if type(v) is not str:
print('数据类型不合法')
# if isinstance(v, str):
return
self.__name = v
@name.deleter
def name(self):
print('不让删')
stu = Student('ly', 18)
print(stu.get_name())
stu.name = 123
print(stu.name)
del stu.name