简体   繁体   English

Python:如何传递对函数的引用

[英]Python: how to pass a reference to a function

IMO python is pass by value if the parameter is basic types, like number, boolean 如果参数是基本类型,如number,boolean,则IMO python按值传递

func_a(bool_value):
    bool_value = True

Will not change the outside bool_value , right? 不会改变外面的bool_value吧?

So my question is how can I make the bool_value change takes effect in the outside one(pass by reference? 所以我的问题是如何让bool_value更改在外部更改生效(通过引用传递?

You can use a list to enclose the inout variable: 您可以使用列表来包含inout变量:

def func(container):
    container[0] = True


container = [False]
func(container)
print container[0]

The call-by-value/call-by-reference misnomer is an old debate. 按值调用/按引用引用的误称是一个古老的争论。 Python's semantics are more accurately described by CLU's call-by-sharing. CLU的call-by-sharing更准确地描述了Python的语义。 See Fredrik Lundh's write up of this for more detail: 有关详细信息,请参阅Fredrik Lundh的相关内容:

Python (always), like Java (mostly) passes arguments (and, in simple assignment, binds names) by object reference . Python(总是),像Java(大多数)一样通过对象引用传递参数(并且,在简单赋值中,绑定名称)。 There is no concept of "pass by value", neither does any concept of "reference to a variables" -- only reference to a value (some express this by saying that Python doesn't have "variables"... it has names , which get bound to values -- and that is all that can ever happen). 没有的“按值传递”的概念,同样没有的“参照变量”任何概念-只有参考 (由一些说,Python 没有 “变数”表达这个......它的名字这与价值观有关 - 这就是所有可能发生的事情。

Mutable objects can have mutating methods (some of which look like operators or even assignment, eg ab = c actually means type(a).__setattr__(a, 'b', c) , which calls a method which may likely be a mutating ones). 可变对象可以有变异方法(其中一些看起来像运算符甚至赋值,例如ab = c实际上意味着type(a).__setattr__(a, 'b', c) ,它调用可能是变异的方法)。

But simple assignment to a barename (and argument passing, which is exactly the same as simple assignment to a barename) never has anything at all to do with any mutating methods. 但简单的赋值给一个barename(和参数传递,这是完全一样的简单分配到barename)从来没有任何事情做任何诱变方法。

Quite independently of the types involved, simple barename assignment (and, identically, argument passing) only ever binds or rebinds the specific name on the left of the = , never affecting any other name nor any object in any way whatsoever. 与所涉及的类型无关,简单的barename赋值(以及相同的参数传递)只会绑定或重新绑定=左侧的特定名称,从不以任何方式影响任何其他名称或任何对象。 You're very mistaken if you believe that types have anything to do with the semantics of argument passing (or, identically, simple assignment to barenames). 如果您认为类型有什么用争论的语义传递(或者,相同的,简单的分配barenames)你是非常错误的。

Unmutable types can't, but if you send a user-defined class instance, a list or a dictionary, you can change it and keep with only one object. 不可变类型不能,但如果您发送用户定义的类实例,列表或字典,则可以更改它并仅保留一个对象。

Like this: 像这样:

def add1(my_list):
    my_list.append(1)

a = []
add1(a)
print a

But, if you do my_list = [1], you obtain a new instance, losing the original reference inside the function, that's why you can't just do "my_bool = False" and hope that outside of the function your variable get that False 但是,如果你执行my_list = [1],你会得到一个新实例,在函数内部丢失原始引用,这就是为什么你不能只做“my_bool = False”并希望你的变量之外的函数得到那个假

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM