python编程:__slots__限制class的属性

作者阿里云代理 文章分类 分类:linux图文教程 阅读次数 已被围观 318

代码示例

# -*- coding: utf-8 -*-  # @File    : slots_demo.py # @Date    : 2018-05-29  class Dog(object):  def __init__(self, name):  self.name = name   class Cat(object):  __slots__ = ["name"]   def __init__(self, name):  self.name = name   if __name__ == '__main__':  d = Dog("dog")  d.age = 23  print(d.age)  # 23   c = Cat("cat")  c.age = 24  # AttributeError: 'Cat' object has no attribute 'age'
本公司销售:阿里云、腾讯云、百度云、天翼云、金山大米云、金山企业云盘!可签订合同,开具发票。

我有话说: