作为一个有 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/