通过引用传递 [英] Passing by reference

查看:76
本文介绍了通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是否正确?


x =" some string"


x是对some string的引用


foo(x)


参考传递给函数。


foo:

x + ="更改


字符串是不可变的,因此foo()中的x现在指向不同的字符串

而不是x foo()外部。

对吗?


回到foo外面。


x = [" some string"]


x是对列表的引用,其第一个元素是对

字符串的引用。


foo(x)

foo内:


x [0] + ="其他


另一个字符串被创建,x的第一个元素被修改为指向

到新字符串并返回foo()外部,x [0 ]将指向新的

字符串。


对吗?

解决方案

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


以下是否正确?


[很多引用引用]


到目前为止一切都很好。


x [0] + ="其他


另一个字符串被创建,x的第一个元素被修改为指向

到新字符串并返回foo()外部,x [0 ]将指向新的

字符串。



更改这些以讨论引用再次,它也将是真的:


"另一个字符串被创建,x的第一个元素现在指的是

新字符串并返回在foo()之外,x仍然是对同一列表的引用(因此它的第一个元素是对同一个

字符串的引用)。


对吗?



对。在Python中,所有名称和容器对象的所有元素,

都是对相应对象的引用。 Python没有

" pointers"的概念。类似C语言的风格。


-

\我驾驶Air Bizarre。你买单程往返组合|

` \门票。离开任何星期一,他们会在星期五把你带回前一个|

_o__)。那样你还有周末。 - 史蒂芬赖特|

Ben Finney


12月21日上午5:57,MartinRineh ... @ gmail.com写道:


以下是否正确?


x =" some string"


x是对某些字符串的引用


foo(x)


参考传递给函数。

在foo:

x + ="更改


字符串是不可变的,因此foo()中的x现在指向不同的字符串

而不是x foo()外部。

对吗?


回到foo外面。


x = [" some string"]


x是对列表的引用,其第一个元素是对

字符串的引用。


foo(x)

foo内:


x [0] + ="其他


创建了另一个字符串,x的第一个元素被修改为点



有点口语/缩写。 x是参考。它没有

元素。你的意思是...... x

所引用的列表的第一个元素被修改......


to新的字符串和后面的foo(),x [0]将指向新的

字符串。


对吗?



足够接近。


....列表的第一个元素x refer是对新字符串的引用,并且返回foo外部,列表的第一个元素是

,x引用将是对新字符串的引用。


对吗?


Is the following correct?

x = "some string"

x is a reference to "some string"

foo(x)

Reference is passed to function.

In foo:
x += " change"

Strings are immutable, so x in foo() now points to a different string
than x outside foo().
Right?

Back outside foo.

x = ["some string"]

x is a reference to a list whose first element is a reference to a
string.

foo(x)

Within foo:

x[0] += " other"

Another string is created, the first element of x is modified to point
to the new string and back outside foo(), x[0] will point to the new
string.

Right?

解决方案

Ma************@gmail.com writes:

Is the following correct?

[lots of references to "references"]

All good so far.

x[0] += " other"

Another string is created, the first element of x is modified to point
to the new string and back outside foo(), x[0] will point to the new
string.

Change these to talk about "references" again and it''ll be true also:

"Another string is created, the first element of x now refers to
the new string and back outside foo(), x is still a reference to
the same list (so its first element is a reference to the same
string)."

Right?

Right. In Python, all names, and all elements of container objects,
are references to the corresponding objects. Python has no concept of
"pointers" in the style of C-like languages.

--
\ "I fly Air Bizarre. You buy a combination one-way round-trip |
`\ ticket. Leave any Monday, and they bring you back the previous |
_o__) Friday. That way you still have the weekend." -- Steven Wright |
Ben Finney


On Dec 21, 5:57 am, MartinRineh...@gmail.com wrote:

Is the following correct?

x = "some string"

x is a reference to "some string"

foo(x)

Reference is passed to function.

In foo:
x += " change"

Strings are immutable, so x in foo() now points to a different string
than x outside foo().
Right?

Back outside foo.

x = ["some string"]

x is a reference to a list whose first element is a reference to a
string.

foo(x)

Within foo:

x[0] += " other"

Another string is created, the first element of x is modified to point

Somewhat colloquial/abbreviated. x is a reference. It does not have
elements. You mean "... the first element of the list to which x
refers is modified ...".

to the new string and back outside foo(), x[0] will point to the new
string.

Right?

Close enough.


.... the first element of the list to which x refers is a reference to
the new string and back outside foo, the first element of the list to
which x refers will be a reference to the new string.

Right?


这篇关于通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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