可变范围2 - 感谢1。 [英] Variable Scope 2 -- Thanks for 1.

查看:59
本文介绍了可变范围2 - 感谢1。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中找到了这个问题:


a = [1,2,3];

b = a;

b.append(4);


b == [1,2,3,4]; #应该如此。

a == [1,2,3,4]; # - 为什么?


有人会认为b是对...的反对 - 但我知道它不是。

无需改变上面的东西,以下是真的:


b = [];

b.append(5);

a == [1, 2,3,4];

b == [5];


如何避免加入修改变量,这是一个错误吗?如果没有

为什么不呢?


Jens Thiede。


顺便说一句,住在南非,那里我们有11个国家

语言很不错 - 结果 - 世界不知道

怎么想我们。 :)

I found the querk in my code:

a = [1, 2, 3];
b = a;
b.append(4);

b == [1, 2, 3, 4]; # As it should.
a == [1, 2, 3, 4]; # - Why?

One would think that b is a referance to a - however I know it''s not.
Without changing a thing from above, the following is true:

b = [];
b.append(5);
a == [1, 2, 3, 4];
b == [5];

How do I avoid accedentaly modifying variables, is this a bug? If not
why not?

Jens Thiede.

By the way, living in South Africa, where we have 11 national
languages is nice although - as a result - the World does not know
what to think of us. :)

推荐答案

Jens Thiede< je ******** @ webgear.co.za>写道:
Jens Thiede <je********@webgear.co.za> wrote:
我在我的代码中找到了querk:
a = [1,2,3];
b = a;
b.append(4);
b == [1,2,3,4]; #应该如此。
a == [1,2,3,4]; # - 为什么?
有人会认为b是对a的反对 - 但是我知道它不是。


相反,a和b保持对同一对象的引用 - 通过表达式[1,2,3]创建列表



如果不改变上面的内容,则以下情况属实:
b = [];
b.append(5);
a == [1,2,3, 4];
b == [5];
如何避免加入修改变量,这是一个错误吗?如果没有
为什么不呢?
I found the querk in my code: a = [1, 2, 3];
b = a;
b.append(4); b == [1, 2, 3, 4]; # As it should.
a == [1, 2, 3, 4]; # - Why? One would think that b is a referance to a - however I know it''s not.
Rather, a and b hold references to the same object--the list created
by the expression [1, 2, 3].
Without changing a thing from above, the following is true: b = [];
b.append(5);
a == [1, 2, 3, 4];
b == [5]; How do I avoid accedentaly modifying variables, is this a bug? If not
why not?




赋值将变量重新绑定到不同的对象,所以b现在持有对创建的列表的

引用通过表达式[]。



Assignment rebinds variables to different objects, so b now holds a
reference to the list created by the expression [].


Jens Thiede写道:
Jens Thiede wrote:
我在代码中找到了querk:
< br => a = [1,2,3];
b = a;
b.append(4);


首先:请删除;从你的行结束......


b == [1,2,3,4]; #应该如此。
a == [1,2,3,4]; # - 为什么?

有人会认为b是对...的反对 - 但我知道它不是。


不是。 a和b都是相同的

列表对象[1,2,3]的名称(或引用)。 (注意细微差别!)


如果不改变上面的东西,以下情况属实:

b = [];
b。追加(5);
a == [1,2,3,4];
b == [5];

如何避免加入修改变量,是这个bug?如果没有
为什么不呢?
I found the querk in my code:

a = [1, 2, 3];
b = a;
b.append(4);
First: please remove the ; from the end of your lines...

b == [1, 2, 3, 4]; # As it should.
a == [1, 2, 3, 4]; # - Why?

One would think that b is a referance to a - however I know it''s not.
It''s not. a and b are both a name (or a reference to) the same
list object [1,2,3]. (notice the subtle difference !)

Without changing a thing from above, the following is true:

b = [];
b.append(5);
a == [1, 2, 3, 4];
b == [5];

How do I avoid accedentaly modifying variables, is this a bug? If not
why not?




这不是一个错误。这就是Python的工作方式:-)

Python的一个非常重要的概念是你没有变量

赋值,但名字捆绑。 赋值语句绑定

对象上的名称,该对象可以是任何类型。这样的陈述:


age = 29

并没有将值29分配给变量年龄。相反,它用名称age标记整数

对象29。完全相同的对象可以被赋予许多名称,

,即许多不同的变量。可以引用相同的值对象:


firstName = login = recordName =" phil"


所有三个名称现在指的是相同的字符串对象phil。因为Python中的赋值

以这种方式工作,所以也没有(类型)声明。您可以在需要时随意引入一个新名称,并将其附加到任何

对象(任何类型),并将其附加到另一个对象(任何类型)当你感觉像是b $ b。例如,如果上面的行已经执行,那么我们就这样做了
login = 20030405


名称登录现在引用一个int对象20030405,而firstName和

recordName仍然引用旧字符串phil。


这就是为什么b在你的第二个例子中被改变了。 b成为

的名称不同的列表对象(即空列表[])。 a仍然是指

原始列表。


HTH,

- Irmen。



It''s not a bug. It''s the way Python works :-)

A very important concept with Python is that you don''t have variable
assignment, but name binding. An "assignment statement" binds a name on an
object, and the object can be of any type. A statement like this:

age = 29

doesn''t assign the value 29 to the variable age. Rather, it labels the integer
object 29 with the name age. The exact same object can be given many names,
that is, many different "variables" can refer to the same value object:

firstName = login = recordName = "phil"

All three names now refer to the same string object "phil". Because assignment
in Python works this way, there are also no (type)declarations. You can
introduce a new name when you want, where you want, and attach it to any
object (of any type), and attach it to another object (of any type) when you
feel like it. For instance, if the line above has been executed and we then do

login=20030405

the name login now refers to an int object 20030405, while firstName and
recordName still refer to the old string "phil".

That''s why b in your second example, is changed. b becomes a name for
a different list object (namely, the empty list []). a still refers to
the original list.

HTH,
--Irmen.


Jens Thiede在留言中写道......
Jens Thiede wrote in message ...
我在代码中找到了一个querk:

a = [1,2,3];
b = a;
b.append(4);

b == [1,2,3,4]; #应该如此。
a == [1,2,3,4]; # - 为什么?

有人会认为b是对...的反对 - 但我知道它不是。
I found the querk in my code:

a = [1, 2, 3];
b = a;
b.append(4);

b == [1, 2, 3, 4]; # As it should.
a == [1, 2, 3, 4]; # - Why?

One would think that b is a referance to a - however I know it''s not.




通常我会写一个大的定制解释完成

例子。但是:

http:// starship.python.net/crew/mwh/...jectthink.html


足够好。


- -

Francis Avila



Ordinarily I would write a big custom-made explaination complete with
examples. However:

http://starship.python.net/crew/mwh/...jectthink.html

This is good enough.

--
Francis Avila


这篇关于可变范围2 - 感谢1。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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