每个类在实例化时都会被分配一个dict,通过 实例.__dict__来访问,dict记录了实例的所有属性

如:

class Man(object):
pass man = Man()
print(man.__dict__) # 输出的结果是 {}
man.name = 'Hel'
man.length = '152'
print(man.__dict__) # 输出的结果是 {'name': 'Hel', 'length': '152'}

变量__slots__是class从object继承的一个属性,用来定义类的可以绑定的属性,当在类中定义了__slots__之后,这个类就只能拥有定义的属性,同时该类的实例不能分配__dict__.

class Man(object):
__slots__ = ('age', 'length', 'name') print(man.__dict__) # 输出AttributeError: 'Man' object has no attribute '__dict 类的实例只能有slots定义的属性,如果使用定义之外的属性,将会报错
class Man(object):
__slots__ = ('age', 'length', 'name') man = Man()
man.name = 'Hel'
man.length = '152'
man.gender = 'male' # 输出AttributeError: 'Man' object has no attribute 'gender'

一般情况下,使用__slots__的类需要直接继承(object)

在继承自己创建的类时,不会继承__slots__属性

Python __slots__的使用的更多相关文章

  1. Python __slots__限制动态添加变量

    Python是一种非常灵活的动态语言,有时感觉太灵活以至于不知道遵循什么样的规则去驾驭.不过Python已经是非常完备的语言,想实现什么样的功能都是有方法的,而且也很容易,比如限制一个类动态添加成员变 ...

  2. python __slots__使用详解

    1.动态添加属性 class Lang(object): def __init__(self,name,score): self.name=name self.score=score def lang ...

  3. python __slots__ 详解(上篇)

    转自:http://blog.csdn.net/sxingming/article/details/52892640 python中的new-style class要求继承Python中的一个内建类型 ...

  4. Python __slots__

    Python 类的特殊变量:__slots__ 使用 __slots__ 后,类中 __weakref__ 和 __dict__ 消失,同时阻止动态属性绑定 由于 __dict__ 记录着类中所有的属 ...

  5. Python __slots__ 作用

    参考:https://blog.csdn.net/u010733398/article/details/52803643   https://blog.csdn.net/sxingming/artic ...

  6. 链表(python)

    链表1.为什么需要链表顺序表的构建需要预先知道数据大小来申请连续的存储空间,而在进行扩充时又需要进行数据的搬迁,所以使用起来并不是很灵活.链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理. ...

  7. Python—使用__slots__限制实例的属性

    如果我们想要限制实例的属性怎么办?比如,只允许对Student实例添加name和age属性. 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该 ...

  8. Python的高级特性6:使用__slots__真的能省很多内存

    在伯乐在线上看到了这篇文章,用Python的 __slots__ 节省9G内存,于是想测试下,对单个类,用__slots__节省内存效果会不会明显. 看完这个例子后,我们也会明白__slots__是用 ...

  9. [Python] dir() 与 __dict__,__slots__ 的区别

    首先需要知道的是,dir() 是 Python 提供的一个 API 函数,dir() 函数会自动寻找一个对象的所有属性,包括搜索 __dict__ 中列出的属性. 不是所有的对象都有 __dict__ ...

随机推荐

  1. Centos7部署open-falcon 0.2

    参考: https://www.cnblogs.com/straycats/p/7199209.html http://book.open-falcon.org/zh_0_2/quick_instal ...

  2. 创建自己的docker基础镜像

    1.下载镜像 centos7 docker pull centos: 2.创建容器加载镜像 docker run -i -t --name centos7 centos: docker run 参数详 ...

  3. Java Web相关概念调查

  4. html 相对路径 问题

    在jsp跳转servlet和servlet跳转jsp过程中,因为servlet和jsp在不同的目录下,所以直接跳转失败.下面是查阅网上的资料,简单的总结下相对路径的问题. 这种情况下index.jsp ...

  5. Effective Java (ENUM篇)

    我们存放一些静态变量,像是一些变量和设置,等等等等,我们尽量使用ENUM,因为ENUM是不可实例化和继承的,所以他很安全,它是在程序一开始运行的时候进行一些编译,修改ENUM不需要再次编译. 在什么时 ...

  6. PODOFO编译

    由于LibHaru库只能创建PDF,所以只能换了. PODOFO项目的依赖项目有: FreeType2: https://sourceforge.net/projects/freetype/files ...

  7. 2019-03-19-day014-内置函数

    昨日回顾 装饰器 对扩展开放 对修改封闭 不改变原调用方式 def a(c): def b(*args,**kwargs): c(*args,**kwargs) return b a() def a( ...

  8. Oracal

    增删改查 1.增加数据表 Create table users ( userid VARCHAR2(4), username VARCHAR2(20), userpass VARCHAR2(20), ...

  9. todolist待办事项

    使用html/css原生js实现待办事项列表: 支持添加待办事项,删除待办事项,切换待办事项的状态(正在进行,已经完成) 支持对正在进行以及已经完成事项编辑(单击内容即可编辑) 源代码:链接:http ...

  10. [LeetCode&Python] Problem 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...