通过引用或按值传递? [英] Pass by reference or by value?

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

问题描述




我在Python中注意到所有函数参数似乎都是通过

引用传递的。这意味着当我修改

函数变量的值时,该函数外部变量的值也是

也被修改。


有时我希望使用副本,因为当我将一个

整数变量传入函数时,我希望函数能够修改

a COPY,不是参考。这可能吗?


谢谢。

Hi,

I noticed in Python all function parameters seem to be passed by
reference. This means that when I modify the value of a variable of a
function, the value of the variable externally from the function is
also modified.

Sometimes I wish to work with "copies", in that when I pass in an
integer variable into a function, I want the function to be modifying
a COPY, not the reference. Is this possible?

Thanks.

推荐答案

7月13日,2:10下午,Robert Dailey< rcdai ... @ gmail.comwrote:
On Jul 13, 2:10 pm, Robert Dailey <rcdai...@gmail.comwrote:




我在Python中注意到所有函数参数似乎通过

引用传递。这意味着当我修改

函数变量的值时,该函数外部变量的值也是

也被修改。


有时我希望使用副本,因为当我将一个

整数变量传入函数时,我希望函数能够修改

a COPY,不是参考。这可能吗?


谢谢。
Hi,

I noticed in Python all function parameters seem to be passed by
reference. This means that when I modify the value of a variable of a
function, the value of the variable externally from the function is
also modified.

Sometimes I wish to work with "copies", in that when I pass in an
integer variable into a function, I want the function to be modifying
a COPY, not the reference. Is this possible?

Thanks.



更正:


我再运行了几次测试,而python实际上是按值传递的,

表示复制。并且传入的外部变量

保持不变。我实际上想知道如何通过

reference,因为对

函数内的参数所做的任何更改也会改变传入的变量。 br />

谢谢。

Correction:

I ran a few more tests and python actually does a pass by value,
meaning that a "copy" is made and the external variable that was
passed in remains unchanged. I actually want to know how to "pass by
reference", in that any changes made to a parameter inside of a
function also changes the variable passed in.

Thanks.


周五,2007-07-13 19:22 +0000,Robert Dailey写道:
On Fri, 2007-07-13 at 19:22 +0000, Robert Dailey wrote:

更正:


我跑了几个测试,python实际上是按值传递的,

意味着复制是
Correction:

I ran a few more tests and python actually does a pass by value,
meaning that a "copy" is made



这显然是不正确的。 Python *中的函数调用从不*生成任何参数的

副本。

That is patently incorrect. A function call in Python *never* makes a
copy of any of its arguments.



传入的
保持不变。
and the external variable that was
passed in remains unchanged.



这完全取决于变量对象是什么类型的对象。是和你如何试图改变变量。

That depends entirely on what kind of object the "variable" is and how
you''re trying to change the "variable."


我其实想知道如何通过

reference",因为对

函数内的参数所做的任何更改也会更改传入的变量。
I actually want to know how to "pass by
reference", in that any changes made to a parameter inside of a
function also changes the variable passed in.



Python是Pass By Reference。总是。期间。


误解的根源在于你不明白

赋值语句在Python中是如何工作的。你来自C的世界,其中一个变量是预定义的存储位置,并且a = 1;表示将'/ b $ b值'写入'''''的内存位置,

删除之前在此内存中的所有内容位置。


Python实际上并没有变量。 Python有对象和

命名空间,赋值语句的工作方式也大不相同。这已经被广泛讨论过了。有关详细信息,请参阅
http://effbot.org/zone /python-objects.htm 和线程理解

python函数这就是昨天在同一个名单上开始的。


HTH,


-

Carsten Haese
http://informixdb.sourceforge.net

Python is Pass By Reference. Always. Period.

The root of your misunderstanding is that you don''t understand how the
assignment statement works in Python. You come from the world of C where
a variable is a predefined memory location and "a=1;" means "Write the
value ''1'' into the memory location that is inhabited by ''a'',
obliterating any contents that were previously in this memory location."

Python doesn''t actually have variables. Python has objects and
namespaces, and assignment statements work very differently. This has
been widely discussed before. For more information, see
http://effbot.org/zone/python-objects.htm and the thread "Understanding
python functions" that started just yesterday on this very same list.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net


Robert Daileyaécrit:
Robert Dailey a écrit :

7月13日下午2:10,Robert Dailey< rcdai ... @ gmail.comwrote:
On Jul 13, 2:10 pm, Robert Dailey <rcdai...@gmail.comwrote:

>>

我注意到在Python中所有的函数参数似乎都是通过
引用传递的。
>>Hi,

I noticed in Python all function parameters seem to be passed by
reference.



(snip)

(snip)


>

更正:


我跑了几个测试,python实际上是按值传递的,
>
Correction:

I ran a few more tests and python actually does a pass by value,



(snip)


还是错的! - )


Python传递对象的引用,但* names *是本地的。所以当

*变异*(即:调用一个改变状态的方法)时,一个对象在
a函数中会影响函数外的对象,* rebinding * a

名称不会影响函数外的名称。


但无论如何,正如Star所提到的,Ben Finney给了一个相当不错的(恕我直言)
$附近线程中的b $ b解释名为了解python函数。

(snip)

Still wrong !-)

Python passes references to objects, but the *names* are local. So while
*mutating* (ie: calling a method that changes the state of) an object in
a function will impact the object outside the function, *rebinding* a
name will not impact the name outside the function.

But anyway, as Star mentionned, Ben Finney gave a pretty good (IMHO)
explanation in the nearby thread named "understanding python functions".


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

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