python - 赋值运算符左侧/右侧的不同切片行为

标签 python python-3.x operators slice deep-copy

作为一个有 C++ 背景的 Python 新手,Python (3.4.x) 中的切片运算符在我看来很可笑。我只是不明白“特殊规则”背后的设计理念。让我解释一下为什么我说它“特别”。

一方面,根据Stack Overflow的回答here切片运算符创建列表或列表的一部分的(深)副本,即新列表。该链接可能较旧(早于 python 3.4.x),但我只是通过以下使用 python 3.4.2 的简单实验确认了该行为:

words = ['cat', 'window', 'defenestrate']
newList = words[:] # new objects are created; a.k.a. deep copy
newList[0] = 'dog'

print(words) # ['cat' ...
print(newList) # ['dog' ...

另一方面,根据官方文档here :

Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:
>>>

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters 
[]

显然,切片操作符 [:] 在这里没有进行深拷贝。

从观察来看,似乎表明当切片运算符位于赋值运算符的左侧/右侧时,它会产生不同的行为。我不知道运算符(operator)可以产生类似行为的任何语言。毕竟,运算符是一个函数,只是一个语法上特殊的函数,函数的行为应该是独立的,完全由其所有输入决定。

那么什么可以证明 Python 设计哲学中的这个“特殊规则”呢?

附言如果我的结论不正确,那真的只有两种可能:

1,Python的切片运算符实际上不是运算符,所以我的假设不成立---那它是什么(切片运算符[:])?

2,行为的差异是由一些未观察到的潜在因素引起的。切片运算符相对于赋值运算符的位置(左/右手侧)意外地与不同行为的观察共存。它们之间没有因果关系---那么导致行为差异的潜在因素是什么?

最佳答案

Python 运算符最好被视为“魔法” 方法的语法糖;例如,x + y 被计算为 x.__add__(y)。以同样的方式:

  • foo = bar.baz 变为 foo = bar.__getattr__(baz);而
  • bar.baz = foo 变为 bar.__setattr__(baz, foo)

Python “切片运算符” * a[b] 被评估为:

  • a.__getitem__(b);或
  • a.__setitem__(b, ...);

取决于它在作业的哪一侧;这两个并不完全相同(另见 How assignment works with python list slice )。以“速写”写出,因此:

>>> x = [1, 2, 3]
>>> x.__getitem__(slice(None))  # ... = x[:]
[1, 2, 3]
>>> x.__setitem__(slice(None), (4, 5, 6))  # x[:] = ...
>>> x
[4, 5, 6]

data model documentation更详细地解释了这些方法(例如 __getitem__ ),您可以阅读 the docs on slice ,也是。


请注意,切片是一个浅拷贝,而不是深拷贝,如下所示:

>>> foo = [[], []]
>>> bar = foo[:]
>>> bar is foo
False  # outer list is new object
>>> bar[0] is foo[0]
True  # inner lists are same objects
>>> bar[0].append(1)
>>> foo
[[1], []]

* 嗯,不是严格 operator .

关于python - 赋值运算符左侧/右侧的不同切片行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30220736/

相关文章:

具有多个列表和 if else 条件的 Python 列表理解

python - 循环 web.client.getPage 函数时内存泄漏

python - 接口(interface)和 Python

c - 运算符优先级和演变

php - 奇数和偶数(使用 & 或 %)

python - 解释嵌套列表理解的工作原理?

python - 全局变量的值在 python 中的函数调用之间丢失

python - 删除输出中的括号和引号

python - 使用 Flask 格式化结果页面

c++ - 如何使用 C++ 17 检查 operator != 是否存在模板参数?