追加对Python中重新定义变量 [英] Append vs. redefining variable in Python

查看:189
本文介绍了追加对Python中重新定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我preface我的道歉问题,如果这已经回答了其他地方。我查阅了一些的问题的堆栈溢出的建议,但他们都不包含我正在寻找的答案。我也比较感兴趣的为什么的这比任何变通办法发生以避免该问题。我试着回答我的问题,但我只能够减少问题简单的。不管怎么说,能有人帮我明白下面这样我能理解推理为不同的输出两套code的区别呢?

First let me preface my question with an apology if this has been answered somewhere else. I reviewed a few of the questions Stack Overflow suggested, but none of them contained the answer I'm looking for. I am also more interested in why this is happening than any workarounds to avoid the problem. I tried answering my own question, but I was only able to reduce the problem to a simpler one. Anyway, can someone please help me understand the difference between the two sets of code below that way I can understand the reasoning for the different outputs?

版本1(使用附加的变量):

Version 1 (using append on the variable dog):

cat = [ ]
dog = [ ]
dog.append(1)
print dog # [1]
print cat # [ ]        
cat.append(dog)        
print dog # [1]
print cat # [ [1] ]
dog.append(1)
print dog # [1, 1]
print cat # [ [1, 1] ]        
dog.append(1)        
print dog # [1, 1, 1]        
print cat # [ [1, 1, 1] ]
cat.append(dog)        
print dog # [1, 1, 1]
print cat # [ [1, 1, 1], [1, 1, 1] ]

版本2(重新定义为不同的列表):

Version 2 (redefining dog as a different list):

cat = [ ]
dog = [ ]
dog = [1]
print dog # [1]
print cat # [ ]
cat.append(dog)
print dog # [1]
print cat # [ [1] ]
dog = [1, 1]
print dog # [1, 1]
print cat # [ [1] ]
dog = [1, 1, 1]
print dog # [1, 1, 1]
print cat # [ [1] ]
cat.append(dog)
print dog # [1, 1, 1]
print cat # [ [1], [1, 1, 1] ]

我在Python的append方法的认识幼稚使我想到第1版具有相同的输出为2版,我不明白为什么追加将影响变量以任何方式,除非我明确更改通过追加或其他方式。

My naïve understanding of the append method in Python led me to expect Version 1 to have the same output as Version 2. I don't understand why appending dog would affect the variable cat in any way unless I explicitly changed cat via append or some other way.

推荐答案

这是很多编程语言,不只是蟒蛇的特征。
这更是一般称为按引用传递(google一下)。

This is a feature of many programming languages, not just python. This is more generally called passing by reference (google it).

在Python中,可变数据结构一般是按引用传递(如列表/字典等),而不变的数据结构(如元组/字符串/整数)作为副本通过。
因此,在code片段#1,当你做 cat.append(狗)第一次,猫现在有狗列表的引用。 (如果你是从C ++ / C的背景下,我可以松散比较这对指针的概念:不是C ++有参考过,所以我说松散...)。

In Python, mutable data-structures are generally passed by reference (such as list/dict etc) while immutable data-structures (such as tuples/strings/ints) are passed as copies. So in code snippet #1, when you do cat.append(dog) the first time around, cat now has a reference to the dog list. (if you are from a C++/C background, I can "loosely" compare this to the concept of pointers: not c++ has reference too, so i say loosely...).

如果它仍然是复杂的,认为具有的地址的。如果修改将具有相同的变化。

If its still complicated, think of cathaving the "address" of dog. If dog changes, cat will have the same changes.

来到您的code段#2 ...

Coming to your code snippet #2...

当您重新定义,你基本上是让指向不同的名单完全。所以 [1,1] [1] 是两个不同的名单完全。

When you redefine dog, you are essentially making dog point to a different list altogether. so [1,1] and [1] are two different lists completely.

要进一步澄清这一概念(可变V / S一成不变),尝试一个稍微不同的运动...

To clarify this concept further (of mutable v/s immutable), try a slightly different exercise...

def func_mutable(dog):
    dog.append(1)

def func_immutable(strng):
    strng+="1"

dog = []
print dog #prints []
func_mutable(dog)
print dog #prints [1]

strng = "1"
print strng #prints "1"
func_immutable(strng)
print strng #still prints "1", and not "11"

函数调用后的狗的变化,因为该列表包含引用和参考的所有更改的狗都体现(即,函数的调用者)。
strng 不改变时func_immutable确实 strng + =1,它实际上是制作副本,B / C传递的参数strng,然后修改它。这些修改都作出了的本地的变量 strng ,如果没有返回(如在我的code的情况下)不敌

dog changes after the function call because the list holds the reference and all changes made to the reference are reflected in the dog(i.e. the callee of the function). strng does not change, b/c when func_immutable does strng+="1", its actually making a copy of the passed parameter strng, and then modifying it. That modification is made to the local variable strng and lost if not returned(as is the case in my code)

这篇关于追加对Python中重新定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆