h9u*_*est 3 python operators deep-copy slice python-3.x
作为来自C++背景的Python新手,Python(3.4.x)中的切片运算符对我来说看起来很荒谬.我只是没有得到"特殊规则"背后的设计理念.让我解释为什么我说它是"特殊的".
一方面,根据此处的Stack Overflow答案,切片运算符创建列表或列表的一部分(深度)副本,即新列表.链接可能是旧的(早于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' ...
Run Code Online (Sandbox Code Playgroud)
另一方面,根据这里的官方文件:
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
[]
Run Code Online (Sandbox Code Playgroud)
显然,切片操作符[:]不会在此处进行深层复制.
从观察结果看,切片算子相对于赋值算子在左/右侧产生不同的行为.我不知道运营商可以产生类似行为的任何语言.毕竟,运算符是一个函数,只是一个语法特殊的函数,函数的行为应该是自包含的,完全由它的所有输入决定.
那么什么可以证明Python设计哲学中的这个"特殊规则"呢?
PS如果我的结论不正确,那么实际上只有两种可能性:
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, ...); 取决于它所在的任务的哪一方; 两者不完全相同(另请参见赋值如何与python列表切片一起使用).因此,用"手写"写出来:
>>> x = [1, 2, 3]
>>> x.__getitem__(slice(None)) # ... = x[:]
[1, 2, 3]
>>> x.__setitem__(slice(None), (4, 5, 6)) # x[:] = ...
>>> x
[4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
该数据模型文档详细介绍了(例如,这些方法__getitem__),你可以阅读的文档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], []]
Run Code Online (Sandbox Code Playgroud)
*嗯,严格来说不是运营商.
| 归档时间: |
|
| 查看次数: |
662 次 |
| 最近记录: |