【python】详解类class的通过__slots__限制类实例对象的属性(七)

当我们通过一个类创建了实例之后,仍然可以给实例添加属性,但是这些属性只属于这个实例。有些时候,我们可以需要限制类实例对象的属性,这时就要用到类中的_ _slots _ _ 属性了。_ _ slots_ _属性对于一个tuple,只有这个tuple中出现的属性可以被类实例使用

class person(object):
    __slots__ = ("name", "age","weight")
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
Bruce = person("Bruce", 25,60)        
print("%s is %d years old and he weights %s" %(Bruce.name, Bruce.age,Bruce.weight))
Bruce.tall = 180

输出结果:

Bruce is 25 years old and he weights 60
Traceback (most recent call last):

  File "<ipython-input-28-88e87a97194a>", line 1, in <module>
    runfile('C:/Users/BruceWong/.spyder-py3/类的__slots__.py', wdir='C:/Users/BruceWong/.spyder-py3')

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/BruceWong/.spyder-py3/类的__slots__.py", line 17, in <module>
    Bruce.tall = 180

AttributeError: 'person' object has no attribute 'tall'

person类实例化后,Bruce不能添加新的属性,_ _ slots_ _属性对于一个tuple属性赋值,只有这个tuple中出现的属性可以被类实例使用

  • 使用_ _ slots _ _ 要注意, _ slots_ _定义的属性仅对当前类的实例起作用,对继承的子类实例是不起作用的,不会限制继承的子类实例化再添加新的属性
class human(object):
    __slots__ = ("name", "age","weight")
class person(human):
    #__slots__ = ("name", "age","weight")
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
Bruce = person("Bruce", 25,60)        
print("%s is %d years old and he weights %s" %(Bruce.name, Bruce.age,Bruce.weight))
Bruce.tall = 180

输出结果,此时添加Bruce.tall =180 属性就不会报错:

Bruce is 25 years old and he weights 60
  • 如果子类本身也有_ _ slots_ _ 属性,子类的属性就是自身的 _ _ slots _ _ 加上父类的_ _ slots_ _
class human(object):
    __slots__ = ("tall")
class person(human):
    __slots__ = ("name", "age","weight")
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
Bruce = person("Bruce", 25,60)        
print("%s is %d years old and he weights %s" %(Bruce.name, Bruce.age,Bruce.weight))
Bruce.tall = 180
print("%s is %d years old and he weights %s and he\'s tall is %s" %(Bruce.name, Bruce.age,Bruce.weight,Bruce.tall))
Bruce.appearance = 'handsome'

输出结果:

Bruce is 25 years old and he weights 60
Bruce is 25 years old and he weights 60 and he's tall is 180
Traceback (most recent call last):

  File "<ipython-input-36-88e87a97194a>", line 1, in <module>
    runfile('C:/Users/BruceWong/.spyder-py3/类的__slots__.py', wdir='C:/Users/BruceWong/.spyder-py3')

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/BruceWong/.spyder-py3/类的__slots__.py", line 26, in <module>
    Bruce.appearance = 'handsome'

AttributeError: 'person' object has no attribute 'appearance'

父类和子类限制的元组里面的变量属性都是可以生成调用的。appearance既不在父类也不再继承的子类的元组列表里,不能生成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值