python class __slots__
来源:互联网 发布:阜宁县网络发言人平台 编辑:程序博客网 时间:2024/05/20 03:39
1,由于Python是动态语言,任何实例在运行过程中,都可以动态地添加属性,python中通过slots 来限制添加属性。
如下代码:
class Student(objetc): __slots__ = ('name', 'gender', 'score') def __init__(self, name, gender, score): self.name = name self.gender = gender self.score = score >>> s = Student('Bob', 'male', 59)>>> s.name = 'Tim' # OK>>> s.score = 99 # OK>>> s.grade = 'A'Traceback (most recent call last): ...AttributeError: 'Student' object has no attribute 'grade'
slots 的目的是限制当前类所拥有的属性。
对于继承类的限制属性添加如下:
class Person(object): __slots__ = ('name', 'gender') def __init__(self, name, gender): self.name = name self.gender = genderclass Student(Person): __slots__ = ('score',) def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = score
0 0
- python class __slots__
- python __slots__
- python __slots__
- Python __slots__
- Python---__slots__
- python __slots__
- python __slots__ 使用
- python的__slots__
- python使用__slots__
- python的__slots__
- python __slots__方法
- Python的__slots__
- Python的__slots__
- Python @property __slots__
- Python __slots__ 作用
- 【python】__slots__简介
- python中 __slots__
- 17 Python __slots__
- FZU 2168 防守阵地 I(公式推导)(经典)(中等)
- ngx_listening_s
- modle factory
- R语言支持向量机SVM
- 开发团队周总结流水20150724
- python class __slots__
- 线段树_HDU_1754
- Android ndk 单步调试
- 关于caffe-windows中 compute_image_mean.exe出现的问题
- 效率极低人群之七大习惯------写得很透彻
- 关于iOS push一些问题的理解
- HDU 1232 畅通工程
- 测试人员如何减少产品的遗留bug
- Spark Streaming 流计算优化记录(3)-控制流量与join的地点