2.4中非常奇怪的问题 [英] very strange problem in 2.4

查看:56
本文介绍了2.4中非常奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题(非常基本,但很奇怪):


我有一个包含一组对象的列表,每个对象有5个vars

和适当的获取或修改变量的功能。当

中的对象列表具有相同的变量(对于所有变量和对象,全部= 5表示var" a" all = 10表示

var" b" )并且我改变了


self.mylist [i] .change_var_a(5)


到一个新值,在这种情况下var"一个"在对象i到5中,现在所有vars都是

类型a我列表中的所有对象都更改为5而不仅仅是var

" a"在对象mylist [i]中,这是我的目标。


如果我在更改vara后立即打印self.mylist [i] .return_var_a()
对象i中的
,我得到了正确的更改,但是当我打印出

整个列表时,最后一次更改为vara在最后修改的对象中

接管列表中的所有对象。


注意:所有未修改的变量必须在所有
中相同列表中的
对象,被修改的var不需要与列表中的

相同(但只会是其中一个相同的

对象已更改)。在最后一个对象var中更改的值

接管了所有对象变量,使它们完全相同。


****如果,例如,一半该列表包含随机变量init的对象。

和另一半相同,如上所述,我执行相同的

操作,如上所述,对同一个var对象


self.mylist [i] .change_var_a(5)(对于

列表中具有相同内容的对象)


所有相同的内容都以与上面相同的方式更改,但是具有不同var值的

对象保持不变。


什么是python在做什么?我错过了什么吗?任何想法都会很好吗?

解决方案

你的列表可能包含几个相同的引用对象,

而不是几个不同的对象。当您使用

技术时,通常会发生这种情况:


list = [object] * 100


。因为虽然这确实在对象时复制。是一个整数,它只是在其他情况下提供引用。

co ************ @ gmail.com 写道:

问题(非常基本,但很奇怪):

我有一个包含一组对象的列表,每个对象有5个vars
和适当的功能来获取或修改变量。当列表中的对象具有相同的变量时(对于所有变量和对象,对于var" all" all = 10,对于
var" b")并且我更改

self.mylist [i] .change_var_a(5)

到一个新值,在这种情况下vara在对象i到5中,现在所有的vars类型都是a。我列表中的所有对象都更改为5而不仅仅是var
a在对象mylist [i]中,这是我的目标。

如果我在对象i中更改vara
后立即打印self.mylist [i] .return_var_a()我得到了正确的更改,但是当我打印出
整个列表时,最后一次更改为vara。在最后修改的对象中,
接管列表中的所有对象。

注意:所有未修改的变量必须在列表中的所有对象中相同, var被修改不需要与列表中的一个相同(但只会改变一个相同的
对象)。在最后一个对象var中更改的值接受了所有对象变量,使它们完全相同。

****例如,如果列表的一半具有随机变量init的对象。
和另一半是完全相同的,如上所述,我执行相同的操作,如上所述,对一个相同的var对象

self.mylist [i] .change_var_a(5)(对于在
列表中具有相同内容的对象)

所有相同的内容都以与上面相同的方式进行更改,但是具有不同的
对象var值没有变化。

什么是python做的?我错过了什么吗?任何想法都会很棒?



On Fri,2006年4月7日21:18:12 -0400,John Zenger写道:

您的列表可能包含对同一对象的多个引用,而不是几个不同的对象。当你使用像

list = [object] * 100
时,这种情况经常发生。因为虽然这确实在对象时复制了是一个整数,它只是在其他情况下引用。




错误。它总是提供参考。

L = [1] * 3
id(L [0] ),id(L [1]),id(L [2])
(155972920,155972920,155972920)


这也不是缓存问题,它对于没有缓存的物品也会发生:

x = 4591034.56472
y = 4591034.56472
x == y
真x是y
假L = [x] * 3
x是L [0]是L [1]是L [2]
真y是L [0]



False


-

史蒂文。


John Zenger写道:

你的列表可能包含对同一个对象的几个引用,而不是几个不同的对象。当你使用

list = [object] * 100




时,这种情况经常发生这很可能是什么还在继续。致OP:请发布

相关代码,包括如何创建mylist以及

change_var_a和return_var_a的定义。我怀疑你正在做类似

这样的事情:

C类(对象):

def __init __(self,x):

self.x = x

def __repr __(self):

返回''C(%r)''%self.x

mylist = [C(0)] * 3 + [C(1)] * 3
mylist
[C(0),C(0),C(0),C(1),C(1),C(1)] mylist [0] .x = 2
[C( 2),C(2),C(2),C(1),C(1),C(1)]

当你应该做的事情:

mylist = [C(0)for i in range(3)] + [C(1)for i in range(3)]
[C(0),C(0),C (0),C(1),C(1),C(1)] mylist [0] .x = 2



[C( 2),C(0),C(0),C(1),C(1),C(1)]


--Ben


The Problem (very basic, but strange):

I have a list holding a population of objects, each object has 5 vars
and appropriate funtions to get or modify the vars. When objects in
the list have identical vars (like all = 5 for var "a" and all = 10 for
var "b" across all vars and objects) and i change

self.mylist[i].change_var_a(5)

to a new value, in this case var "a" in object i to 5, now all vars of
type "a" in all objects in my list are changed to 5 instead of just var
"a" in object mylist[i], which is my goal.

if i print self.mylist[i].return_var_a() right after I change var "a"
in object i, I get the correct change, however when i print out the
whole list, the last change to var "a" in the last object modified
takes over for all objects in the list.

note: all the vars not being modified must be the same across all
objects in the list, the var being modified need not be the same as the
one before it in the list (but will be once just one of the identical
object are changed). The value changed in the last object var modified
takes over for all object vars making them exactly identical.

****If, for example, half the list has objects with random vars init.
and the other half is identical, as above, and I perform the same
operation, as above, to one of the identical var objects

self.mylist[i].change_var_a(5) (to an object that has identicals in the
list)

all the identicals are changed in the same way as above, however the
objects that have different var values are unchanged.

What is python doing? Am I missing something? Any ideas at all would be
wonderful?

解决方案

Your list probably contains several references to the same object,
instead of several different objects. This happens often when you use a
technique like:

list = [ object ] * 100

...because although this does make copies when "object" is an integer, it
just makes references in other cases.

co************@gmail.com wrote:

The Problem (very basic, but strange):

I have a list holding a population of objects, each object has 5 vars
and appropriate funtions to get or modify the vars. When objects in
the list have identical vars (like all = 5 for var "a" and all = 10 for
var "b" across all vars and objects) and i change

self.mylist[i].change_var_a(5)

to a new value, in this case var "a" in object i to 5, now all vars of
type "a" in all objects in my list are changed to 5 instead of just var
"a" in object mylist[i], which is my goal.

if i print self.mylist[i].return_var_a() right after I change var "a"
in object i, I get the correct change, however when i print out the
whole list, the last change to var "a" in the last object modified
takes over for all objects in the list.

note: all the vars not being modified must be the same across all
objects in the list, the var being modified need not be the same as the
one before it in the list (but will be once just one of the identical
object are changed). The value changed in the last object var modified
takes over for all object vars making them exactly identical.

****If, for example, half the list has objects with random vars init.
and the other half is identical, as above, and I perform the same
operation, as above, to one of the identical var objects

self.mylist[i].change_var_a(5) (to an object that has identicals in the
list)

all the identicals are changed in the same way as above, however the
objects that have different var values are unchanged.

What is python doing? Am I missing something? Any ideas at all would be
wonderful?



On Fri, 07 Apr 2006 21:18:12 -0400, John Zenger wrote:

Your list probably contains several references to the same object,
instead of several different objects. This happens often when you use a
technique like:

list = [ object ] * 100

..because although this does make copies when "object" is an integer, it
just makes references in other cases.



Wrong. It always makes references.

L = [1]*3
id(L[0]), id(L[1]), id(L[2]) (155972920, 155972920, 155972920)

This isn''t a caching issue either, it also happens for objects which
aren''t cached:
x = 4591034.56472
y = 4591034.56472
x == y True x is y False L = [x]*3
x is L[0] is L[1] is L[2] True y is L[0]


False

--
Steven.


John Zenger wrote:

Your list probably contains several references to the same object,
instead of several different objects. This happens often when you use a
technique like:

list = [ object ] * 100



This is most likely what''s going on. To the OP: please post the
relevant code, including how you create mylist and the definitions of
change_var_a and return_var_a. I suspect you''re doing something like
this:

class C(object):
def __init__(self, x):
self.x = x
def __repr__(self):
return ''C(%r)'' % self.x
mylist = [C(0)]*3 + [C(1)]*3
mylist [C(0), C(0), C(0), C(1), C(1), C(1)] mylist[0].x = 2 [C(2), C(2), C(2), C(1), C(1), C(1)]

When you should do something like:
mylist = [C(0) for i in range(3)] + [C(1) for i in range(3)] [C(0), C(0), C(0), C(1), C(1), C(1)] mylist[0].x = 2


[C(2), C(0), C(0), C(1), C(1), C(1)]

--Ben


这篇关于2.4中非常奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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