“一切”是一种尊重还是不是? [英] Is 'everything' a refrence or isn't it?

查看:47
本文介绍了“一切”是一种尊重还是不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设python中的所有内容都是一个参考...


所以,如果我编码:

lst = [1,2 ,3]

for i in lst:

if i == 2:

i = 4

print lst


我虽然会修改lst的内容..(看完之后

''所有''是一个参考。)

因此,为了做到这一点,我需要编写代码:


lst = [1,2,3]

for i在范围(len(lst)):

如果lst [i] == 2:

lst [i] = 4

print lst


我误解了什么吗?

解决方案

KraftDiner写道:

我假设python中的所有东西都是参考...


这是真的。

所以如果我编码:
lst = [1,2,3]


lst是对包含对1,2和3的引用的列表的引用

for i在lst:


i是对列表中每个元素的引用,另一个是另一个。

但这不再是一个参考,列表被取消引用
$ b在获取其内容之前$ b,并且仅返回

返回。

如果i == 2:
i = 4




在这个周期中你现在有我作为4的参考。


ciao - chris


-

Christian Tismer:^)< mailto:ti **** @ stackless.com>

tismerysoft GmbH:休息一下!搭乘Python'

Johannes-Niemeyer-Weg 9A:* Starship * http://starship.python.net/

14109柏林:PGP密钥 - > http://wwwkeys.pgp.net/

work +49 30 802 86 56手机+49 173 24 18 776传真+49 30 80 90 57 05

PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04

谁你想今天赞助吗? http://www.stackless.com/


2006-01-04,KraftDiner< bo ******* @ yahoo.com>写道:

我假设python中的所有内容都是一个参考...


它是。

所以,如果我编码:
lst = [1,2,3]
我在lst:
如果i == 2:
i = 4
print lst

我虽然会修改lst的内容..


不。 i = 4没有修改对象。它改变了i。

指向另一个对象(一个整数值为4)。

(读完之后
''所有''是一个参考。)
所以似乎为了做到这一点,我需要编写代码:

lst = [1,2,3]
我在范围内(len(lst) ):
如果lst [i] == 2:
lst [i] = 4
打印lst

我误解了什么吗?




排序。


你误解了i = 4这句话的含义。意味着。


-

格兰特爱德华兹格兰特哇!我会得到它的!!这可能是我b / b
visi.com意大利女孩朋友的价格。


KraftDiner写道:

我假设python中的所有东西都是一个参考...


它是,尽管它更好从名称和绑定的角度思考。

所以,如果我编码:
lst = [1,2,3]
我在lst:
如果我== 2:
我= 4
打印lst

我虽然会修改lst的内容..(看完之后
''一切''是参考。)


执行此代码时,名称我将被绑定为1,2,3和4.

列表元素被绑定到1,2和3.重新绑定''我'这样,而不是

引用2它现在引用4不会影响第二个
$列表中的b $ b元素引用2.

所以看来为了做到这一点,我需要d如下编码:

lst = [1,2,3]
我在范围内(len(lst)):
如果lst [i] == 2 :
lst [i] = 4
打印lst

我误解了什么吗?




显然。


I was under the assumption that everything in python was a refrence...

so if I code this:
lst = [1,2,3]
for i in lst:
if i==2:
i = 4
print lst

I though the contents of lst would be modified.. (After reading that
''everything'' is a refrence.)
so it seems that in order to do this I need to code it like:

lst = [1,2,3]
for i in range(len(lst)):
if lst[i] == 2:
lst[i]=4
print lst

Have I misunderstood something?

解决方案

KraftDiner wrote:

I was under the assumption that everything in python was a refrence...
This is true.
so if I code this:
lst = [1,2,3]
lst is a reference to a list that holds references to 1, 2, and 3
for i in lst:
i is a reference to every element in the list, one ofter the other.
But this is not a ref to lst any longer, the list gets dereferenced
before getting at its contents, and only references to that are
returned.
if i==2:
i = 4



You now have i as a reference to 4, during this cycle.

ciao - chris

--
Christian Tismer :^) <mailto:ti****@stackless.com>
tismerysoft GmbH : Have a break! Take a ride on Python''s
Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/


On 2006-01-04, KraftDiner <bo*******@yahoo.com> wrote:

I was under the assumption that everything in python was a refrence...
It is.
so if I code this:
lst = [1,2,3]
for i in lst:
if i==2:
i = 4
print lst

I though the contents of lst would be modified..
Nope. "i = 4" doesn''t modify the object. It changes "i" to
point to a different object (one that is an integer value 4).
(After reading that
''everything'' is a refrence.)
so it seems that in order to do this I need to code it like:

lst = [1,2,3]
for i in range(len(lst)):
if lst[i] == 2:
lst[i]=4
print lst

Have I misunderstood something?



Sort of.

You''ve misunderstood what the statement "i = 4" means.

--
Grant Edwards grante Yow! I''LL get it!! It''s
at probably a FEW of my
visi.com ITALIAN GIRL-FRIENDS!!


KraftDiner wrote:

I was under the assumption that everything in python was a refrence...
It is, although it is better to think in terms of names and bindings.

so if I code this:
lst = [1,2,3]
for i in lst:
if i==2:
i = 4
print lst

I though the contents of lst would be modified.. (After reading that
''everything'' is a refrence.)
During execution of this code, the name ''i'' is bound to 1, 2, 3 and 4. the
list elements are bound to 1, 2, and 3. Rebinding ''i'' such that instead of
referencing 2 it now references 4 doesn''t affect the fact that the second
element of the list references 2.
so it seems that in order to do this I need to code it like:

lst = [1,2,3]
for i in range(len(lst)):
if lst[i] == 2:
lst[i]=4
print lst

Have I misunderstood something?



Evidently.


这篇关于“一切”是一种尊重还是不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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