- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
作为一个有 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/
我正在使用这段代码: border-right:0px solid #ddd; height:85px; box-shadow :5px 5px 10px 1px #eaeaea; 但我刚刚得到
我遇到的问题是将文本放在图像的左侧... HTML 和 CSS h2, social-hand { font-family: 'PT Sans', sans-serif; display: i
美好的一天 我今天的 CSS 很糟糕。我有 3 列 div(水平,这就是我使用 float 的原因),但我想将我的文本居中对齐,但在包含列 div 的左侧。 HTML:
在我的应用程序中,我在截取屏幕截图时在我的 Imageview 中获得了透明像素。如何剪切顶部、左侧、右侧 和 Imageview 的底部。我尝试了下面的代码但没有工作 Bitmap bitmap =
如何在左侧的以下 LinearLayout 内部添加边框,使其宽度为 5dp 并适合 LinearLayout 的整个高度? LinearLayout 的高度是动态的,这意味着有时它可以是 50dp
我是第一次做网站。我在使用下拉菜单时遇到问题。它正在向左侧移动。请帮助我处理代码,使其位于导航菜单的正下方,并且将鼠标悬停在其上时应该可见。提前致谢。 这是我的 CSS 代码: ul { li
我对 html 和 css 有点生疏,我无法让 2 个图像 float ,一个在另一个之上。这是代码...   &nbs
我有一个固定宽度 (px) 的左侧边栏,右侧是我的主要内容。 这是我的演示:http://jsfiddle.net/fxWg7/4031/ 代码如下: 这是我的 html 代码: left co
所以我正在尝试做一个弹出式菜单,但我遇到了一点问题。 我已经申请了: .menu-side, .menu { -webkit-transition: left 0.2s ease; -
在下面的 CSS @media screen and (max-width: 750px) { .flexbox { display: block; } .menu ul {
@import 'https://fonts.googleapis.com/css?family=PT+Sans'; * { font-family: 'PT Sans', sans-serif
我想在我的标题旁边放一个图标。但它不合适..我怎样才能让它合适? My heading 最佳答案 使用vertical-align:middle,因此如果您更改文本或图像的大小,位
我正在使用参数方程围绕一个圆定位图像 ( How do I calculate a point on a circle’s circumference? ) 我使用简单的数学方法以更少的方式完成此操作
我想使用外部列表属性将 ul 列表正确定位在左浮动 img 的右侧,但元素符号未在图像右侧对齐,因为没有任何图像,但更多在左边。 Lorem ipsum dolor sit amet, c
我有一个layer-list.xml 现在我想在运行时更改 (top,right) 等项目的
我通过按:M-x linum-mode 来启用它们。如何将其“翻译”到 my.emacs 文件中,以便每次打开 EMACS 时自动启用它? 最佳答案 如果您希望每个缓冲区都使用它,请将 (global
我的Code : HTML 1 2 3 CSS #score { height:50px; } .myDiv { width:40px; height:40px;
我可以轻松设置 canvas.width 和 canvas.height 属性。我找不到正确的 JavaScript 语法来使用顶部和左侧设置 Canvas 的偏移量。我尝试过直接在 Canvas 上
我正在尝试为我正在编写的单人纸牌游戏完成条件游戏。 我把所有的 Action 和棋子都移除了。每一 block 都作为椭圆 UI 元素保存在一个二维数组中,当一 block 被拿走时,它会被替换为边框
我正在使用导航 View 修改 Android 应用程序。对于前几个菜单项,我给出了一个图标,但之后,我只给出了菜单标题。但是在导航面板中,没有图标的菜单项有一个空白区域。 如何删除空白区域?我希望菜
我是一名优秀的程序员,十分优秀!