python中__slots__用法实例
作者:MaxOmnis 时间:2022-05-29 17:49:22
本文实例讲述了python中__slots__的用法。分享给大家供大家参考。具体分析如下:
定义__slots__ 后,可以再实例上分配的属性名称将被限制为指定的名称。否则将引发AttributeError,这种限制可以阻止其他人向现有的实例添加新的属性.
使用__slots__的类的实例不在使用字典来存储数据。相反,会使用基于数组的更加紧凑的数据结构。
在会创建大量对象的程序中,使用__slots__可以显著减少内存占用和使用时间
class Account(object):
__slots__ = ('name' ,'balance')
class Test(object):
def __init__(self ,name):
self.name = name
希望本文所述对大家的Python程序设计有所帮助。
标签:python,slots
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用内置json模块解析json格式数据的方法
2023-07-30 14:10:45
![](https://img.aspxhome.com/file/2023/6/61226_0s.png)
Python神经网络TensorFlow基于CNN卷积识别手写数字
2022-06-18 20:23:00
![](https://img.aspxhome.com/file/2023/3/99433_0s.png)
细品Dreamweaver MX 2004内建FW技术
2010-09-02 12:38:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113037414s.gif)
在linux系统下安装python librtmp包的实现方法
2021-05-08 21:13:50
msxml3.dll 错误 ‘800c0005’解决方案
2009-10-05 18:36:00
解析:怎样在MySQL中获得更好的搜索结果
2008-11-27 15:19:00
基于Django框架的权限组件rbac实例讲解
2022-09-27 17:11:51
![](https://img.aspxhome.com/file/2023/2/72032_0s.jpg)
值得收藏的10道python 面试题
2022-04-22 12:10:33
![](https://img.aspxhome.com/file/2023/6/84076_0s.png)
python requests.post带head和body的实例
2022-07-12 01:22:59
浅谈DataFrame和SparkSql取值误区
2021-03-21 22:04:18
![](https://img.aspxhome.com/file/2023/8/85208_0s.jpg)
Python scrapy增量爬取实例及实现过程解析
2022-11-12 21:58:39
利用Python将list列表写入文件并读取的方法汇总
2023-12-07 13:40:07
![](https://img.aspxhome.com/file/2023/4/95164_0s.png)
详解Numpy数组转置的三种方法T、transpose、swapaxes
2023-01-14 21:42:13
![](https://img.aspxhome.com/file/2023/7/77667_0s.png)
认清区别CSS的类class和id
2007-10-08 12:02:00
PyQt5实现进度条与定时器及子线程同步关联
2023-03-20 14:48:35
![](https://img.aspxhome.com/file/2023/5/88875_0s.gif)
python实现RSA加密(解密)算法
2023-04-20 06:12:15
![](https://img.aspxhome.com/file/2023/8/107088_0s.png)
隐藏你的.php文件的实现方法
2023-10-20 22:58:01
python根据照片获取地理位置及泄露防御
2022-03-23 11:16:54
![](https://img.aspxhome.com/file/2023/8/112408_0s.png)
SQLServer 2008助你轻松编写T-SQL存储过程
2010-12-06 13:38:00
![](https://img.aspxhome.com/file/UploadPic/201012/201012621374263s.jpg)
jupyter notebook运行代码没反应且in[ ]没有*
2022-03-06 17:37:39
![](https://img.aspxhome.com/file/2023/1/87551_0s.png)