本文介绍了了解 Python 传递函数参数的按对象调用风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否理解 Python 通过对象样式传递函数参数的调用概念(在此处解释 http://effbot.org/zone/call-by-object.htm).似乎没有足够的例子来很好地阐明这个概念(或者我的 google-fu 可能很弱!:D)

I am not sure I understand the concept of Python's call by object style of passing function arguments (explained here http://effbot.org/zone/call-by-object.htm). There don't seem to be enough examples to clarify this concept well (or my google-fu is probably weak! :D)

我写了这个小小的人为 Python 程序来试图理解这个概念

I wrote this little contrived Python program to try to understand this concept

def foo( itnumber, ittuple,  itlist, itdict   ):
    itnumber +=1
    print id(itnumber) , itnumber

    print id(ittuple)  , ittuple

    itlist.append(3.4)
    print id(itlist)   , itlist

    itdict['mary']  = 2.3
    print id(itdict),    itdict



# Initialize a number, a tuple, a list and a dictionary
tnumber = 1
print id( tnumber ), tnumber

ttuple  = (1, 2, 3)
print id( ttuple ) , ttuple

tlist   = [1, 2, 3]
print id( tlist ) , tlist

tdict = tel = {'jack': 4098, 'sape': 4139}
print '-------'
# Invoke a function and test it
foo(tnumber, ttuple, tlist , tdict)

print '-------'
#Test behaviour after the function call is over
print id(tnumber) , tnumber
print id(ttuple)  , ttuple
print id(tlist)   , tlist
print id(tdict),  tdict

程序的输出为

146739376 1
3075201660 (1, 2, 3)
3075103916 [1, 2, 3]
3075193004 {'sape': 4139, 'jack': 4098}

---------

146739364 2
3075201660 (1, 2, 3)
3075103916 [1, 2, 3, 3.4]
3075193004 {'sape': 4139, 'jack': 4098, 'mary': 2.3}

---------

146739376 1
3075201660 (1, 2, 3)
3075103916 [1, 2, 3, 3.4]
3075193004 {'sape': 4139, 'jack': 4098, 'mary': 2.3}

如您所见,除了传递的整数外,对象 ID(据我所知是指内存位置)保持不变.

As you can see , except for the integer that was passed, the object id's (which as I understand refers to memeory location) remain unchanged.

所以在整数的情况下,它(有效地)通过值传递,而其他数据结构(有效地)通过引用传递.我尝试更改列表、数字和字典以测试数据结构是否已更改到位.数字不是列表和字典是.

So in the case of the integer, it was (effectively) passed by value and the other data structure were (effectively) passed by reference. I tried changing the list , the number and the dictionary to just test if the data-structures were changed in place. The number was not bu the list and thedictionary were.

我在上面使用了有效这个词,因为参数传递的按对象调用"风格似乎是双向的,这取决于上面代码中传递的数据结构

I use the word effectively above, since the 'call-by-object' style of argument passing seems to behave both ways depending on the data-structure passed in the above code

对于更复杂的数据结构(比如 numpy 数组等),是否有任何快速的经验法则识别哪些参数将通过引用传递,哪些参数将通过值传递?

For more complicated data structures, (say numpy arrays etc), is there any quick rule of thumb torecognize which arguments will be passed by reference and which ones passed by value?

推荐答案

关键区别在于,在 C 风格的语言中,变量是内存中的一个盒子,您可以在其中放置东西.在 Python 中,变量是一个名称.

Python 既不是按引用调用也不是按值调用.这是更明智的事情!(其实我是先学Python再学比较常用的语言,所以call-by-value和call-by-reference对我来说很陌生.)

Python is neither call-by-reference nor call-by-value. It's something much more sensible! (In fact, I learned Python before I learned the more common languages, so call-by-value and call-by-reference seem very strange to me.)

在 Python 中,有事物名称.列表、整数、字符串和自定义对象都是事物.xyz 是名称.写作

In Python, there are things and there are names. Lists, integers, strings, and custom objects are all things. x, y, and z are names. Writing

x = []

的意思是构造一个新事物 [] 并给它命名 x".写作

means "construct a new thing [] and give it the name x". Writing

x = []
foo = lambda x: x.append(None)
foo(x)

的意思是构造一个名为x的新事物[],构造一个名为foo的新函数(这是另一回事),并在名称为 x 的事物上调用 foo".现在 foo 只是将 None 附加到它收到的任何内容,因此这简化为将 None 附加到空列表".写作

means "construct a new thing [] with name x, construct a new function (which is another thing) with name foo, and call foo on the thing with name x". Now foo just appends None to whatever it received, so this reduces to "append None to the the empty list". Writing

x = 0
def foo(x):
    x += 1
foo(x)

表示构造一个名为x的新事物0,构造一个新的函数foo,并调用foo> 在 x 上".在 foo 中,赋值只是说将 x 重命名为 1 加上它以前的样子",但这并没有改变 thing 0.

means "construct a new thing 0 with name x, construct a new function foo, and call foo on x". Inside foo, the assignment just says "rename x to 1 plus what it used to be", but that doesn't change the thing 0.

这篇关于了解 Python 传递函数参数的按对象调用风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:08