在Python中使用__slots__方法的详细教程
作者:廖雪峰 时间:2022-10-30 09:46:10
正常情况下,当我们定义了一个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, Student) # 给实例绑定一个方法
>>> 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 = MethodType(set_score, None, Student)
给class绑定方法后,所有实例均可调用:
>>> s.set_score(100)
>>> s.score
100
>>> s2.set_score(99)
>>> s2.score
99
通常情况下,上面的set_score方法可以直接定义在class中,但动态绑定允许我们在程序运行的过程中动态给class加上功能,这在静态语言中很难实现。
使用__slots__
但是,如果我们想要限制class的属性怎么办?比如,只允许对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
Try
除非在子类中也定义__slots__,这样,子类允许定义的属性就是自身的__slots__加上父类的__slots__。
标签:Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pycharm远程连接服务器并配置python interpreter的方法
2023-03-21 20:36:06
![](https://img.aspxhome.com/file/2023/7/82897_0s.png)
解决Jupyter Notebook开始菜单栏Anaconda下消失的问题
2021-04-09 18:33:05
![](https://img.aspxhome.com/file/2023/7/83857_0s.jpg)
Python通过递归函数输出嵌套列表元素
2021-03-23 15:36:09
![](https://img.aspxhome.com/file/2023/2/133842_0s.png)
Centos8(最小化安装)全新安装Python3.8+pip的方法教程
2022-11-09 06:00:27
浅谈Go语言并发机制
2024-05-10 13:58:38
![](https://img.aspxhome.com/file/2023/6/127476_0s.jpg)
SQL Server利用sp_spaceused如何查看表记录存在不准确的情况
2024-01-20 07:40:10
![](https://img.aspxhome.com/file/2023/0/129310_0s.png)
SQL语句导入导出大全
2024-01-25 10:37:39
Dreamweaver MX 2004 制作树状菜单教程[动画]
2010-03-25 12:24:00
如何完美的建立一个python项目
2021-02-20 21:07:26
超简单使用Python换脸实例
2021-07-31 17:03:36
![](https://img.aspxhome.com/file/2023/3/70853_0s.png)
python中二维阵列的变换实例
2021-06-28 07:54:06
远程连接mysql错误代码1130的解决方法
2024-01-14 06:54:05
![](https://img.aspxhome.com/file/2023/4/128074_0s.png)
基于Python编写一个有趣的进程勾选器(Process Selector)
2023-06-19 06:28:19
![](https://img.aspxhome.com/file/2023/6/133476_0s.png)
认识那些被忽略的SQL Server注入技巧
2009-01-20 13:15:00
SQL截取字符串函数分享
2024-01-23 23:09:59
10分钟用Python快速搭建全文搜索引擎详解流程
2023-11-06 16:13:41
![](https://img.aspxhome.com/file/2023/1/67321_0s.png)
使用SQL语句,查第10-20条记录
2008-02-19 18:34:00
Python使用pyautogui模块实现自动化鼠标和键盘操作示例
2022-10-27 16:02:25
MySQL之复杂查询的实现
2024-01-23 00:32:43
![](https://img.aspxhome.com/file/2023/8/122928_0s.jpg)
Node.js中文件操作模块File System的详细介绍
2024-05-13 10:04:47