0
点赞
收藏
分享

微信扫一扫

Python教程: __slots__限制动态添加变量


Python是一种非常灵活的动态语言,有时感觉太灵活以至于不知道遵循什么样的规则去驾驭。不过Python已经是非常完备的语言,想实现什么样的功能都是有方法的,而且也很容易,比如限制一个类动态添加成员变量。

一般情况下,我们定义完一个类,如果不加任何限制,还可以动态地为该类的对象或该类添加成员变量。

class Employee:
def __init__(self,name=''):
self.name = name

if __name__ == "__main__":
try:
employee1 = Employee('Bob')
#动态为一个对象添加成员变量
employee1.tel = '11111111'
print(employee1.name,employee1.tel)
employee2 = Employee('Tom')
#employee2对象没有tel成员变量
print(employee2.name,employee2.tel)
except AttributeError as e:
print(e)
#动态为一个类添加成员变量
Employee.tel = []
print(employee2.name,employee2.tel)

#Bob 11111111
#'Employee' object has no attribute 'tel'
#Tom []

看起来很方便,不过如果我们如果不想使用者破坏类的结构、随便添加成员变量,要怎么做呢?

答案是,可以使用​​__slots__​​对象。

在类中,​​__slots__​​是一个以元组形式被定义的,只有在元组里的属性,才可以被赋值,不在元组里的属性赋值时会报错。

class Employee:
__slots__ = ('name')
def __init__(self,name=''):
self.name = name

if __name__ == "__main__":
employee1 = Employee('Bob')
#动态为一个对象添加成员变量,但不在__slots__定义的元组内,会报异常
employee1.tel = '11111111'
print(employee1.name,employee1.tel)

#AttributeError: 'Employee' object has no attribute 'tel'

​__solts__​​​不能被子类继续,如果想限制子类的成员变量,子类也要定义​​__slots__​​​变量,同时会继承父类的​​__slots__​

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流群:711312441
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class Employee:
__slots__ = ('name')
def __init__(self,name=''):
self.name = name

class Manager1(Employee):
pass
class Manager2(Employee):
__slots__ = ('addr')

if __name__ == "__main__":
manager1 = Manager1('Bill')
#动态为一个对象添加成员变量
manager1.tel = '22222222'
print(manager1.name,manager1.tel)
manager2 = Manager2()
manager2.name = 'David'
manager2.addr = 'BJ'
print(manager2.name,manager2.addr)
#动态为一个对象添加成员变量,不在__slots__里,会报异常
manager2.tel = '33333333'
print(manager2.tel)

#Bill 22222222
#David BJ
#AttributeError: 'Manager2' object has no attribute 'tel'

如果想进一步限制对成员变量的使用,可以重载​​__setattr__​​​, ​​__getattr__​​​,​​__getattribute__​​​函数,​​__setattr__​​​函数可以在赋值前拦截;​​__getattr__​​​在找不到属性时被调用;​​__getattribute__​​​则在获取属性值时无条件被调用,同时​​__getattr__​​​不再被调用。注意不要在​​__getattribute__​​里使用self.attr来访问变量,这会导致无限递归循环。

class Employee:
__slots__ = ('name')
def __init__(self,name=''):
self.name = name

class Manager2(Employee):
__slots__ = ('addr')
def __setattr__(self,name,value):
if name == 'tel':
raise AttributeError('manager has no tel')
object.__setattr__(self, name, value)
def __getattr__(self,name):
if name == 'tel':
return 0
object.__getattribute__(self, name)
if __name__ == "__main__":
manager2 = Manager2('Jone')
manager2.name = 'David'
manager2.addr = 'BJ'
try:
manager2.tel = '11111111'
except Exception as e:
print(e)
print(manager2.name,manager2.addr,manager2.tel)

#manager has no tel
#David BJ 0


举报

相关推荐

0 条评论