分享

Python进阶系列(七)

 zhulin1028 2022-06-20 发布于山东

对象变动(Mutation)

Python中可变(mutable)与不可变(immutable)的数据类型让新手很是头痛。简单的说,可变(mutable)意味着"可以被改动",而不可变(immutable)的意思是“常量(constant)”。想把脑筋转动起来吗?考虑下这个例子:

foo = ['hi']

print(foo)

# Output: ['hi']

bar = foo

bar += ['bye']

print(foo)

# Output: ['hi', 'bye']

刚刚发生了什么?我们预期的不是那样!我们期望看到是这样的:

foo = ['hi']

print(foo)

# Output: ['hi']

bar = foo

bar += ['bye']

print(foo)

# Output: ['hi']

print(bar)

# Output: ['hi', 'bye']

这不是一个bug。这是对象可变性(mutability)在作怪。每当你将一个变量赋值为另一个可变类型的变量时,对这个数据的任意改动会同时反映到这两个变量上去。新变量只不过是老变量的一个别名而已。这个情况只是针对可变数据类型。下面的函数和可变数据类型让你一下就明白了:

def add_to(num, target=[]):

    target.append(num)

    return target

add_to(1)

# Output: [1]

add_to(2)

# Output: [1, 2]

add_to(3)

# Output: [1, 2, 3]

你可能预期它表现的不是这样子。你可能希望,当你调用add_to时,有一个新的列表被创建,就像这样:

def add_to(num, target=[]):

    target.append(num)

    return target

add_to(1)

# Output: [1]

add_to(2)

# Output: [2]

add_to(3)

# Output: [3]

啊哈!这次又没有达到预期,是列表的可变性在作怪。在Python中当函数被定义时,默认参数只会运算一次,而不是每次被调用时都会重新运算。你应该永远不要定义可变类型的默认参数,除非你知道你正在做什么。你应该像这样做:

def add_to(element, target=None):

    if target is None:

        target = []

    target.append(element)

    return target

现在每当你在调用这个函数不传入target参数的时候,一个新的列表会被创建。举个例子:

add_to(42)

# Output: [42]

add_to(42)

# Output: [42]

add_to(42)

# Output: [42]

__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__情况下的精确内存

占用。

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

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

In [4]: from slots import *

In [4] used 9.3008 MiB RAM in 0.72s, peaked 0.00 MiB above current, total RAM

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

In [6]: from noslots import *

In [6] used 22.6680 MiB RAM in 0.80s, peaked 0.00 MiB above current

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多