python基础——使用__slots__
2016-08-29 22:14
232 查看
python基础——使用__slots__
正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性。先定义class:class Student(object): pass
然后,尝试给实例绑定一个属性:
>>> s = Student() >>> s.name = 'Michael' # 动态给实例绑定一个属性 >>> print(s.name) Michael
还可以尝试给实例绑定一个方法:
>>> def set_age(self, age): # 定义一个函数作为实例方法 ... self.age = age ... >>> from types import MethodType >>> s.set_age = MethodType(set_age, s) # 给实例绑定一个方法 >>> s.set_age(25) # 调用实例方法 >>> s.age # 测试结果 25
但是,给一个实例绑定的方法,对另一个实例是不起作用的:
>>> s2 = Student() # 创建新的实例 >>> s2.set_age(25) # 尝试调用方法 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'set_age'
为了给所有实例都绑定方法,可以给class绑定方法:
>>> def set_score(self, score): ... self.score = score ... >>> Student.set_score = set_score
给class绑定方法后,所有实例均可调用:
>>> s.set_score(100) >>> s.score 100 >>> s2.set_score(99) >>> s2.score 99
通常情况下,上面的
set_score方法可以直接定义在class中,但动态绑定允许我们在程序运行的过程中动态给class加上功能,这在静态语言中很难实现。
使用__slots__
但是,如果我们想要限制实例的属性怎么办?比如,只允许对Student实例添加name和
age属性。
为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的
__slots__变量,来限制该class实例能添加的属性:
class Student(object): __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称
然后,我们试试:
>>> s = Student() # 创建新的实例 >>> s.name = 'Michael' # 绑定属性'name' >>> s.age = 25 # 绑定属性'age' >>> s.score = 99 # 绑定属性'score' Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'score'
由于
'score'没有被放到
__slots__中,所以不能绑定
score属性,试图绑定
score将得到
AttributeError的错误。
使用
__slots__要注意,
__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的:
>>> class GraduateStudent(Student): ... pass ... >>> g = GraduateStudent() >>> g.score = 9999
除非在子类中也定义
__slots__,这样,子类实例允许定义的属性就是自身的
__slots__加上父类的
__slots__。
参考源码:
#python 使用__slots__ 示例 #2016-8-29 21:25:14 #MengmengCoding # -*- coding: utf-8 -*- #使用__slots__限制实例的属性 class Student(object): __slots__=('name','age') #用tuple定义允许绑定的属性名称 #新类,继承于Student class GraduateStudent(Student): pass s=Student() #创建新的实例 s.name='Shuke' #绑定属性'name' s.age=25 #绑定属性'age' # ERROR: AttributeError: 'Student' object has no attribute 'score' try: s.score=99 except AttributeError as e: print('AttributeError:',e) g=GraduateStudent() #创建新的实例 ''' 使用__slots__要注意,__slots__定义的属性仅对当前类实例起作用, 对继承的子类是不起作用的 ''' g.score=99 #绑定属性'score' print('g.score=',g.score)
相关文章推荐
- Python基础(6)- 类和对象(使用、继承、派生、组合、接口、多态、封装、property、staticmethod、classmethod、反射、slots、上下文管理协议、元类)
- Python基础-使用__slots__
- Python网络编程基础笔记-使用minidom生成XML文件
- python-math-基础使用
- Linux Python基础知识学习,使用Eclipse与Pydev搭建python开发环境
- python基础教程之lambda表达式使用方法
- python基础教程之python消息摘要算法使用示例
- Python基础学习篇——Global全局变量的使用
- 使用locals()获得类,进行分发 分类: python 小练习 divide into python python基础学习 2014-02-21 14:51 217人阅读 评论(0) 收藏
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- if ...__name__使用技巧总结 分类: python基础学习 python Module python 2013-11-01 14:51 262人阅读 评论(0) 收藏
- python基础教程之类class定义使用方法
- python基础:用apply使用元祖和列表调用函数
- python-math-基础使用
- Python学习入门基础教程(learning Python)--1.2.1 Python输出语句print基本使用 .
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- 在python中使用pdb基础调试
- python数据持久存储:pickle模块的基本使用 分类: python python基础学习 python 小练习 2013-06-17 14:41 209人阅读 评论(0) 收藏
- 对象使用self注意事项 分类: python基础学习 2013-09-03 09:52 218人阅读 评论(0) 收藏
- Python使用基础