Python 函数按引用调用 [英] Python functions call by reference

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

问题描述

在某些语言中,您可以使用refval 等特殊保留字,通过引用或值传递参数.当您将参数传递给 Python 函数时,它永远不会在离开函数时更改参数的值.唯一的方法是使用 global 保留字(或我目前理解的).

In some languages you can pass a parameter by reference or value by using a special reserved word like ref or val. When you pass a parameter to a Python function it never alters the value of the parameter on leaving the function.The only way to do this is by using the global reserved word (or as i understand it currently).

示例 1:

k = 2

def foo (n):
     n = n * n     #clarity regarding comment below
     square = n
     return square

j = foo(k)
print j
print k

会显示

>>4
>>2

显示 k 不变.

在这个例子中,变量 n 永远不会改变

In this example the variable n is never changed

示例 2:

n = 0
def foo():
    global n
    n = n * n
    return n

在这个例子中,变量 n 被改变了.

In this example the variable n is changed.

Python 中是否有任何方法可以调用函数并告诉 Python 参数引用 参数,而不是使用全球?

Is there any way in Python to call a function and tell Python that the parameter is either a value or reference parameter instead of using global?

其次,在剑桥 A 级考试中,他们现在说函数返回单个值,而过程返回多个值.在 80 年代,我被教导有一个函数有一个 return 语句而过程没有.为什么现在不正确?

Secondly, in the A level Cambridge exams they now say a function returns a single value whereas a procedure returns more than one value. I was taught a function has a return statement and procedure does not, during the 80s. Why is this now incorrect?

推荐答案

你不能在 Python 的函数中改变不可变对象,比如 strtuple,但您可以执行以下操作:

You can not change an immutable object, like str or tuple, inside a function in Python, but you can do things like:

def foo(y):
  y[0] = y[0]**2

x = [5]
foo(x)
print x[0]  # prints 25

然而,这是一种奇怪的处理方式,除非您需要始终对数组中的某些元素进行平方.

That is a weird way to go about it, however, unless you need to always square certain elements in an array.

请注意,在 Python 中,您还可以返回多个值,从而使按引用传递的一些用例变得不那么重要:

Note that in Python, you can also return more than one value, making some of the use cases for pass by reference less important:

def foo(x, y):
   return x**2, y**2

a = 2
b = 3
a, b = foo(a, b)  # a == 4; b == 9

当您返回这样的值时,它们将作为元组返回,而元组又会被解包.

When you return values like that, they are being returned as a Tuple which is in turn unpacked.

另一种思考方式是,虽然您不能在 Python 中通过引用显式传递变量,但您可以修改传入的对象的属性.在我的示例(和其他示例)中,您可以修改列表中的成员传入.但是,您无法完全重新分配传入的变量.例如,请看以下两段代码,它们看起来可能会做类似的事情,但最终会得到不同的结果:

edit: Another way to think about this is that, while you can't explicitly pass variables by reference in Python, you can modify the properties of objects that were passed in. In my example (and others) you can modify members of the list that was passed in. You would not, however, be able to reassign the passed in variable entirely. For instance, see the following two pieces of code look like they might do something similar, but end up with different results:

def clear_a(x):
  x = []

def clear_b(x):
  while x: x.pop()

z = [1,2,3]
clear_a(z) # z will not be changed
clear_b(z) # z will be emptied

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

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