gpt4 book ai didi

python - 为什么 python 不利用 __iadd__ 求和和链式运算符?

转载 作者:太空狗 更新时间:2023-10-29 21:07:06 25 4
gpt4 key购买 nike

我刚刚进行了一个有趣的测试:

~$ python3 # I also conducted this on python 2.7.6, with the same result
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
... def __add__(self, other):
... global add_calls
... add_calls += 1
... return Foo()
... def __iadd__(self, other):
... return self
...
>>> add_calls = 0
>>> a = list(map(lambda x:Foo(), range(6)))
>>> a[0] + a[1] + a[2]
<__main__.Foo object at 0x7fb588e6c400>
>>> add_calls
2
>>> add_calls = 0
>>> sum(a, Foo())
<__main__.Foo object at 0x7fb588e6c4a8>
>>> add_calls
6

很明显,__iadd__ 方法比__add__ 方法更高效,不需要分配新的类。如果我添加的对象足够复杂,这将创建不必要的新对象,可能会在我的代码中造成巨大的瓶颈。

我希望,在 a[0] + a[1] + a[2] 中,第一个操作将调用 __add__,第二个操作将调用将在新创建的对象上调用 __iadd__

为什么python不优化这个?

最佳答案

__add__ 方法可以自由返回不同类型的对象,而 __iadd__ 如果使用就地语义,应该返回 self .它们在这里不需要返回相同类型的对象,因此 sum() 不应依赖于 __iadd__ 的特殊语义。

您可以使用 functools.reduce() function自己实现所需的功能:

from functools import reduce

sum_with_inplace_semantics = reduce(Foo.__iadd__, a, Foo())

演示:

>>> from functools import reduce
>>> class Foo(object):
... def __add__(self, other):
... global add_calls
... add_calls += 1
... return Foo()
... def __iadd__(self, other):
... global iadd_calls
... iadd_calls += 1
... return self
...
>>> a = [Foo() for _ in range(6)]
>>> result = Foo()
>>> add_calls = iadd_calls = 0
>>> reduce(Foo.__iadd__, a, result) is result
True
>>> add_calls, iadd_calls
(0, 6)

关于python - 为什么 python 不利用 __iadd__ 求和和链式运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31054393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com