Python __slots__

本文详细介绍了Python中__slots__的使用方法及其作用,解释了如何利用__slots__限制类实例的属性,节省内存并提高性能。此外还讨论了如何通过在属性名前加双下划线实现类属性的保护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python新模式的class,即从object继承下来的类有一个变量是__slots__,slots的作用是阻止在实例化类时为实例分配dict,默认情况下每个类都会有一个dict,通过__dict__访问,这个dict维护了这个实例的所有属性,举例如下
class base(object):
    v = 1
    def __init__(self):
          pass

b = base()
print b.__dict__
b.x = 2                     //可以增加新的变量
print b.__dict__
运行:
{}
{'x':2}
可见:实例的dict只保持实例的变量,对于类的属性是不保存的,类的属性包括变量和函数。由于每次实例化一个类都要分配一个新的dict,因此存在空间的浪费,因此有了slots,当定义了slots后,slots中定义的变量变成了类的描述符,相当于java,c++中的成员变量声明,类的实例只能拥有这些个变量,而不在有dict,因此也就不能在增加新的变量
class base(object):
   __slots__ = ('y')
    v = 1
    def __init__(self):
          pass

b = base()
print b.__dict__
b.x = 2              //error,不能增加新的变量 (任何试图创建一个其名不在__slots__中的名字的实例属性都将导致AttributeError异常)
print b.__dict__

注意,如果类的成员变量与slots中的变量同名,
class base(object):
    __slots=('y',)
   y = 2
    v = 1
    def __init__(self):
          pass

b = base()
print b.__dict__
b.x = 2
b.y = 3 //read only
print b.__dict__

目前的实现是该变量被设置为readonly!!!

Notes on using __slots__

  • Without a __dict__ variable, instances cannot be assigned new variables not listed in the __slots__ definition. Attempts to assign to an unlisted variable name raise an AttributeError. If dynamic assignment of new variables is desired, then add '__dict__' to the sequence of strings in the __slots__ declaration. (Changed in Python version 2.3)
  • Without a __weakref__ variable for each instance, classes defining __slots__ do not support weak references to its instances. If weak reference support is needed, then add '__weakref__' to the sequence of strings in the__slots__ declaration. (Changed in Python version 2.3)
  • __slots__ are implemented at the class level by creating descriptors ( 3.4.2.2) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by __slots__; otherwise, the class attribute would overwrite the descriptor assignment.
  • If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this.
  • The action of a __slots__ declaration is limited to the class where it is defined. As a result, subclasses will have a__dict__ unless they also define __slots__.
  • __slots__ do not work for classes derived from "variable-length" built-in types such as longstr and tuple.
  • Any non-string iterable may be assigned to __slots__. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key.

 

另外:

 由于在Python中不支持private这样的私有化修饰符,所以如果想把一个类属性置为私有的话,方法就是在属性名前加上双下划线__,这样在编译后,就可以起到保护私有属性的作用

    例如:

    

  1. 假如有个类Numstr有一个self.num属性   
  2.     在加上双下划线后变成self.__num后,经过编译就形成了self.__Numstr__num了,可以有效的保护私有属性,  
假如有个类Numstr有一个self.num属性
    在加上双下划线后变成self.__num后,经过编译就形成了self.__Numstr__num了,可以有效的保护私有属性,
  1. 但并非无懈可击,这只能算是一种保护机制  
但并非无懈可击,这只能算是一种保护机制

 

  同时在Python中提供了限制用户创建并未在类中定义的属性,方法是在类的定义后跟一个__slots__属性,它是一个元组,包括了当前能访问到的属性。例如:

   

  1. class test(object):   
  2.     __slots__=('foo');       //可以访问foo
  3.     foo=1.3;   
  4.        
  5. a=test();   
  6. print a.foo;   
  7. a.bar=15  
  8. print a.bar;   
  9.   
  10. 输出:  
class test(object):
    __slots__=('foo');    
    foo=1.3;
    
a=test();
print a.foo;
a.bar=15
print a.bar;

输出:
  1. 1.3  
  2. Traceback (most recent call last):   
  3.   File "D:\pythonEclipse\PythonStudy\src\Unit13\TestSlots.py", line 7in <module>   
  4.     a.bar=15  
  5. AttributeError: 'test' object has no attribute 'bar'  

另外:

Python 是一门动态语言,可以在运行过程中,修改对象的属性和增删方法。任何类的实例对象包含一个字典__dict__, Python通过这个字典将任意属性绑定到对象上。有时候我们只想使用固定的对象,而不想任意绑定对象,这时候我们可以定义一个属性名称集合,只有在这个集合里的名称才可以绑定。__slots__就是完成这个功能的。

test code

1 # !/usr/bin/env python
2 # _*_ coding:utf-8 _*_
3
4 class  test_slot(object):
5 __slots__ = " width " " height "
6
7 def  width(self):
8 print " width "
9
10
11 class  test(object):
12   def  amethod(self):
13   print " amethod "

ipython执行代码导入

In [ 1 ]:  from  test  import *
In [
2 ]: dir(test_slot)
Out[
2 ]:
[
' __class__ ' ,
' __slots__ ' ,
' __str__ ' ,
' height ' ,
' width ' ]

In [
3 ]: dir(test)
Out[
3 ]:
[
' __class__ ' ,
' __weakref__ ' ,
' amethod ' ]

可以看到 test_slot 类结构里面已经有height和width属性了。

下面我们继续查看两个实例对象结构里都有什么吧

In [ 4 ]: s  =  test_slot()
In [
5 ]: t  =  test()

In [
11 ]: dir(s)
Out[
11 ]:
[
' __class__ ' ,
' __delattr__ ' ,
' __doc__ ' ,
' __slots__ ' ,
' height ' ,
' width ' ]

In [
12 ]: dir(t)
Out[
12 ]:
[
' __class__ ' ,
' __delattr__ ' ,
' __dict__ ' ,
' __doc__ ' ,
' amethod ' ]

看到了吧, test_slot 的实例s 没有字典__dict__,多了__slots__属性,这样就不能任意绑定属性,只能绑定__slots__属性名称集合里的同名属性了。

我们来测试一把,先给两个实例的赋__slots__里面有的 height 属性,当然对t来说是新加属性。

In [7]: s.height = 1
In [8]: t.height = 0

赋值都没有问题。

下面再试一下不在__slots__里的属性名.

In [ 9 ]: s.abeen  = " abeen " // 和预期的一样,失败了。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)

/ home / abeen / learn_test / test /< ipython console > in < module > ()

AttributeError: 
' test_slot '  object has no attribute  ' abeen '

In [
10 ]: t.abeen  = " abeen " // 正常运行
内容概要:本文档提供了关于“微型车间生产线的设计与生产数据采集试验研究”的毕业设计复现代码,涵盖从论文结构生成、机械结构设计、PLC控制系统设计、生产数据采集与分析系统、有限元分析、进度管理、文献管理和论文排版系统的完整实现。通过Python代码和API调用,详细展示了各个模块的功能实现和相互协作。例如,利用SolidWorks API设计机械结构,通过PLC控制系统模拟生产流程,使用数据分析工具进行生产数据的采集和异常检测,以及利用进度管理系统规划项目时间表。 适合人群:具有机械工程、自动化控制或计算机编程基础的学生或研究人员,尤其是从事智能制造领域相关工作的人员。 使用场景及目标:①帮助学生或研究人员快速搭建和理解微型车间生产线的设计与实现;②提供完整的代码框架,便于修改和扩展以适应不同的应用场景;③作为教学或科研项目的参考资料,用于学习和研究智能制造技术。 阅读建议:此资源不仅包含详细的代码实现,还涉及多个学科领域的知识,如机械设计、电气控制、数据分析等。因此,在学习过程中,建议读者结合实际操作,逐步理解每个模块的功能和原理,并尝试调整参数以观察不同设置下的系统表现。同时,可以参考提供的文献资料,深入研究相关理论和技术背景。
本次的学生体质健康信息管理网站,按照用户的角色可以分为教师与学生,后台设置管理员角色来对学生的信息进行管理。,设计如下: 1、后台管理系统 后台管理系统主要是为该系统的管理员提供信息管理服务的系统,具体包括的功能模块如下: (1)管理员信息管理 (2)教师信息管理 (3)学生信息管理 (4)健康信息统计(图形化进行健康,亚健康等学生的信息数量统计) 2、教师角色的功能模块设计 教师角色所需要的功能模块主要包括了如下的一些内容: (1)个人资料修改 (2)学生体质健康管理:录入相关数据,包括但不限于身高、体重、肺活量、视力等生理指标以及运动能力、身体成分、骨密度等健康指标,并且设置健康,亚健康状态 (3)学生健康建议:根据体质信息,进行学生健康的建议 (4)健康预警:对健康出问题的学生,进行健康预警 (5)饮食和锻炼情况管理,查看 3、学生角色 学生角色可以通过该信息网站看到个人的基本信息,能够看到教师给与学生的健康建议等,功能模块设计如下: (1)个人资料修改 (2)我的健康建议查看 (3)我的健康预警 (4)饮食和锻炼情况管理,记录平时的饮食和锻炼情况 完整前后端源码,部署后可正常运行! 环境说明 开发语言:Java后端 框架:ssm,mybatis JDK版本:JDK1.8+ 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:eclipse/idea Maven包:Maven3.3+ 部署容器:tomcat7.5+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值