__slots__ 用法,作用,slots魔法,python3


__slots__

现在我们终于明白了,动态语言与静态语言的不同

动态语言:可以在运行的过程中,修改代码

静态语言:编译时已经确定好代码,运行过程中不能修改

如果我们想要限制实例的属性怎么办?比如,只允许对Person实例添加name和age属性。

为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性

Python
>>> class Person(object): __<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/slots" title="View all posts in slots" target="_blank">slots</a></span>__ = ("name", "age") >>> P = Person() >>> P.name = "老王" >>> P.age = 20 >>> P.score = 100 Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> AttributeError: Person instance has no attribute 'score' >>>
1
2
3
4
5
6
7
8
9
10
11
>>> class Person ( object ) :
     __slots__ = ( "name" , "age" )
 
>>> P = Person ( )
>>> P . name = "老王"
>>> P . age = 20
>>> P . score = 100
Traceback ( most recent call last ) :
   File "<pyshell#3>" , line 1 , in < module >
AttributeError : Person instance has no attribute 'score'
>>>

注意:

使用__slots__要注意,__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的

Python
In [67]: class Test(Person): ...: pass ...: In [68]: t = Test() In [69]: t.score = 100
1
2
3
4
5
6
7
In [ 67 ] : class Test ( Person ) :
     . . . :      pass
     . . . :
 
In [ 68 ] : t = Test ( )
 
In [ 69 ] : t . score = 100

在Python中,每个类都有实例属性。默认情况下Python用一个字典来保存一个对象的实例属性。这非常有用,因为它允许我们在运行时去设置任意的新属性。
然而,对于有着已知属性的小类来说,它可能是个瓶颈。这个字典浪费了很多内存。Python不能在对象创建时直接分配一个固定量的内存来保存所有的属性。因此如果你创建许多对象(我指的是成千上万个),它会消耗掉很多内存。
不过还是有一个方法来规避这个问题。这个方法需要使用__slots__来告诉Python不要使用字典,而且只给一个固定集合的属性分配空间。
这里是一个使用与不使用__slots__的例子:
不使用 __slots__:

Python
class MyClass(object): def __init__(self, name, identifier): self.name = name self.identifier = identifier self.set_up() # ... 使用 __slots__: class MyClass(object): __slots__ = ['name', 'identifier'] def __init__(self, name, identifier): self.name = name self.identifier = identifier self.set_up() # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MyClass ( object ) :
   def __init__ ( self , name , identifier ) :
       self . name = name
       self . identifier = identifier
       self . set_up ( )
   # ...
使用 __slots__ :
class MyClass ( object ) :
   __slots__ = [ 'name' , 'identifier' ]
   def __init__ ( self , name , identifier ) :
       self . name = name
       self . identifier = identifier
       self . set_up ( )
   # ...

第二段代码会为你的内存减轻负担。通过这个技巧,有些人已经看到内存占用率几乎40%~50%的减少。+

稍微备注一下,你也许需要试一下PyPy。它已经默认地做了所有这些优化。




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值