python__slots__的禁用实例
python中__slots__的禁用实例
1、说明
Python的对象属性值都是采用字典存储的,当我们处理数成千上万甚至更多的实例时,内存消耗可能是一个问题,因为字典哈希表的实现,总是为每个实例创建了大量的内存。所以Python提供了一种__slots__的方式来禁用实例使用__dict__,以优化此问题。
2、实例
通过__slots__来指定属性后,会将属性的存储从实例的__dict__改为类的__dict__中:
classTest:
__slots__=('a','b')
def__init__(self,a,b):
self.a=a
self.b=b
>>>t=Test(1,2)
>>>t.__dict__
AttributeError:'Test'objecthasnoattribute'__dict__'
>>>Test.__dict__
mappingproxy({'__module__':'__main__',
'__slots__':('a','b'),
'__init__':,
'a':,
'b':,
'__doc__':None})
以上就是Python中__slots__的禁用实例,希望对大家有所帮助。更多Python学习推荐:请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python中s3cmd是什么?
python中s3cmd是什么?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。简单介绍:可以实现使用s3对象存储安装方式:yuminstalls3cmd...详情>>
2023-11-14 19:15:02
python中如何使用pandas实现行数据添加
python中如何使用pandas实现行数据添加python中,pandas是一个数据分析包,可以对数据进行很方便的处理提供了高效地操作大型数据集所需的工具。...详情>>
2023-11-14 16:35:59
python迭代器中Yield方法怎么用?
python迭代器中Yield方法怎么用?Yield用于返回数据,程序执行到yield后,返回结果,记住当前状态,暂停执行,下次调用时,根据上次的状态,返回...详情>>
2023-11-14 16:11:13
python元组拆包如何实现?
python元组拆包如何实现?1、说明把元组一一对应拆出来,就叫做元组拆包。拆包有个要求,元组中的元素数量必须跟接受这些元素的空挡数一致,否则...详情>>
2023-11-14 13:24:22热门推荐
python中any函数如何使用?
沸python operator模块有何用法?
热python中s3cmd是什么?
热python中getattribute方法作用是什么?
新python如何使用TemporaryFile()方法创建临时文件?
python中sys模块是什么?
python的ndarray与pandas的series如何相互转换?
python中如何使用pandas实现行数据添加
python迭代器中Yield方法怎么用?
python中temp是什么意思?
python中Cartopy是什么
python入门:方差和标准差的区别
python切片如何作为占位符使用
python元组拆包如何实现?
技术干货






