意外行为:我创建了一个指针吗? [英] unexpected behavior: did i create a pointer?

查看:47
本文介绍了意外行为:我创建了一个指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

在调试我的代码两天后,我已经明白了问题是由于python的意外行为引起的。
问题。或者缺少

当然有关该计划的一些信息!我已经删除了

代码来重现问题:


< code>

a = {}


for x in range(10):

for y in range(10):

a [x,y] =" 0"


copyOfA = a


def functionA(x,y):

打印一个[x, y],

copyOfA [x,y] =" *"

打印[x,y],copyOfA [x,y]

范围内的x(10):

范围内的y(10):

functionA(x,y)


< / code>

现在,在第二个for中循环和功能A()我只是''触摸''copyOfA

(改变它)。因为我不接触变量a,我希望它不会因任何变化而受到影响,但是copyOfA就像指向a和

的指针一样改变copyOfA'的值会导致改变a的值,所以

结果我期望的是:

0 0 *

0 0 *

0 0 *

0 0 *

[..]


但我得到:

0 * *

0 * *

0 * *

0 * * < br $> b $ b [..]


发生了什么事?

提前感谢。

hi to all!
after two days debugging my code, i''ve come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i''ve stripped down the
code to reproduce the problem:

<code>
a = {}

for x in range(10):
for y in range(10):
a[x,y] = "0"

copyOfA = a

def functionA(x,y):
print a[x,y],
copyOfA[x,y] = "*"
print a[x,y],copyOfA[x,y]
for x in range(10):
for y in range(10):
functionA(x,y)

</code>
now, in the second "for" cycle and in functionA() i only ''touch'' copyOfA
(altering it). as i don''t touch the variable "a", i expect it not to be
affected by any change, but copyOfA acts like a pointer to a and
altering copyOfA''s values result in altering the values of "a", so the
result that i expect is:
0 0 *
0 0 *
0 0 *
0 0 *
[..]

but i get:
0 * *
0 * *
0 * *
0 * *
[..]

what''s going on?
thanks in advance.

推荐答案

En Fri,2007年9月7日05:07:03 -0300,gu< pi ******** @ gmail.comescribi ???:
En Fri, 07 Sep 2007 05:07:03 -0300, gu <pi********@gmail.comescribi???:

调试我的代码两天后,我发现

问题是由python的意外行为引起的。或者缺少

当然有关该计划的一些信息!我已经删除了

代码来重现问题:


< code>

a = {}


for x in range(10):

for y in range(10):

a [x,y] =" 0"


copyOfA = a
after two days debugging my code, i''ve come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i''ve stripped down the
code to reproduce the problem:

<code>
a = {}

for x in range(10):
for y in range(10):
a[x,y] = "0"

copyOfA = a



copyOfA是* NOT *副本 - 它只是另一个指向的名称相同的
对象作为一个。

Python永远不会复制任何东西,除非明确告知。


阅读本文< http:/ /effbot.org/zone/python-objects.htm>


-

Gabriel Genellina

copyOfA is *NOT* a copy - it''s just another name pointing to the SAME
object as a.
Python will never copy anything unless told explicitely.

Read this <http://effbot.org/zone/python-objects.htm>

--
Gabriel Genellina


gu schreef:
gu schreef:

copyOfA = a


现在,在第二个for中循环和功能A()我只是''触摸''copyOfA

(改变它)。
copyOfA = a

now, in the second "for" cycle and in functionA() i only ''touch'' copyOfA
(altering it).



copyOfA不是a的副本;它是一个与同一个

对象绑定的不同名称。您可以验证:id(a)和id(copyOfA)将返回

相同的值。


复制a(假设是(dict),你可以这样做:


copyOfA = dict(a)





copyOfA = a.copy()


或更多一般


导入副本

copyOfA = copy.copy (a)

干杯,

Roel


-

如果我能够再看看,这只是因为我站在巨人的肩膀上。 - Isaac Newton


Roel Schroeven

copyOfA isn''t a copy of a; it''s a different name bound to the same
object as a. You can verify that: id(a) and id(copyOfA) will return the
same value.

To make a copy of a (assuming a is a dict), you can do:

copyOfA = dict(a)

or

copyOfA = a.copy()

or more generally

import copy
copyOfA = copy.copy(a)
Cheers,
Roel

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven


gu写道:
gu wrote:

hi all!

在调试我的代码两天后,我已经说到了这个问题是由于python的意外行为引起的。
问题。或者缺少

当然有关该计划的一些信息!我已经删除了

代码来重现问题:


[snip FAQ]
hi to all!
after two days debugging my code, i''ve come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i''ve stripped down the
code to reproduce the problem:

[snip FAQ]



是的,基本上你*创建了一个指针。这就是python所拥有的:

指针。


当说

Yes, basically you *created* a pointer. That''s all that python has:
pointers.

When saying


>> a = AnyOldObject()
b = a
>>a = AnyOldObject()
b = a



然后''a''和'b''对于/非常相同/对象是不同的/名称/(尝试

" a是b",或者id (a)== id(b)。


这是一个常见问题解答(每周一次左右?),但对于我的生活我是

找不到谷歌查询的正确单词。

给TROOP:你会对这个问题附加什么关键词?


/ W

then ''a'' and ''b'' are different /names/ for the /very same/ object (try
"a is b", or "id(a)==id(b)").

This is really a FAQ (once a week or so?), but for the life of me I
can''t find the right words for a google query.
TO THE TROOP: What keywords would you attach to that question?

/W


这篇关于意外行为:我创建了一个指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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