python __slots__魔法

先谈谈python 中__dict__存储了该对象的一些属性

类和实例分别拥有自己的__dict__

在__init__中声明的变量,会存到实例的__dict__中

类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类__dict__里的

虽然说一切皆对象,但对象也有不同,就好比不是每个人的女朋友都是一个人一样,一些内置的数据类型是没有__dict__属性的

int, list, dict等这些常用的数据类型是没有__dict__属性的,其实这是可预料的,就算给了它们dict属性也没啥用,毕竟它们只是用来做数据容器的。

__slots__魔法

在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()
    # ...

 

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

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

以下你可以看到一个例子,它用IPython来展示在有与没有__slots__情况下的精确内存占用,感谢 https://github.com/ianozsvald/ipython_memory_usage

Python 3.4.3 (default, Jun  6 2015, 13:32:34)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import ipython_memory_usage.ipython_memory_usage as imu

In [2]: imu.start_watching_memory()
In [2] used 0.0000 MiB RAM in 5.31s, peaked 0.00 MiB above current, total RAM usage 15.57 MiB

In [3]: %cat slots.py
class MyClass(object):
        __slots__ = ['name', 'identifier']
        def __init__(self, name, identifier):
                self.name = name
                self.identifier = identifier

num = 1024*256
x = [MyClass(1,1) for i in range(num)]
In [3] used 0.2305 MiB RAM in 0.12s, peaked 0.00 MiB above current, total RAM usage 15.80 MiB

In [4]: from slots import *
In [4] used 9.3008 MiB RAM in 0.72s, peaked 0.00 MiB above current, total RAM usage 25.10 MiB

In [5]: %cat noslots.py
class MyClass(object):
        def __init__(self, name, identifier):
                self.name = name
                self.identifier = identifier

num = 1024*256
x = [MyClass(1,1) for i in range(num)]
In [5] used 0.1758 MiB RAM in 0.12s, peaked 0.00 MiB above current, total RAM usage 25.28 MiB

In [6]: from noslots import *
In [6] used 22.6680 MiB RAM in 0.80s, peaked 0.00 MiB above current, total RAM usage 47.95 MiB

__slots__作用

1.减少内存消耗

2.限制添加类属性及类初始化参数

 

实现__slots__

1.限制单个类属性

复制代码
class Base(object):
    __slots__ = ['name']
  pass

B = Base() B.name = 'John'
复制代码

2.限制多个类属性,在__slots__的列表中继续添加即可

复制代码
class Base(object):
    __slots__ = ['name','age']
  pass

B = Base() B.name = 'John' B.age = 18
复制代码

当添加类属性为非__slots__列表中的参数,会报错 AttributeError: 'Base' object has no attribute 'name1'

复制代码
class Base(object):
    __slots__ = ['name']

    pass
B = Base()
B.name1 = 'John'  #AttributeError: 'Base' object has no attribute 'name1'
复制代码

 

同样,__slots__会限制类初始化参数

复制代码
class Base(object):
    __slots__ = ['name']

    def __init__(self,name,age):
        self.name = name
        self.age = age
    pass

B = Base('John',18) #AttributeError: 'Base' object has no attribute 'age'
复制代码

 

节约内存

由于默认情况下每个类都会有一个dict,通过__dict__访问,这个dict维护了这个实例的所有属性。如果类中使用__slots__时,该对象不会自动产生__dict__属性

复制代码
class Base(object):
    __slots__ = ['name']

    def __init__(self,name):
        self.name = name
    pass

B = Base('John')

print(B.__dict__)  #打印报错AttributeError: 'Base' object has no attribute '__dict__'

class Base(object):
    # __slots__ = ['name']

    def __init__(self,name):
        self.name = name
    pass

B = Base('John')

print(B.__dict__) # {'name': 'John'}
 
 
posted @ 2023-02-21 14:52  左闯  阅读(23)  评论(0编辑  收藏  举报