__slots__
现在我们终于明白了,动态语言与静态语言的不同
动态语言:可以在运行的过程中,修改代码
静态语言:编译时已经确定好代码,运行过程中不能修改
如果我们想要限制实例的属性怎么办?比如,只允许对Person实例添加name和age属性。
为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性:
>>> 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__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的
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__:
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。它已经默认地做了所有这些优化。