Python 中有没有办法通过输出参数返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4702249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way in Python to return a value via an output parameter?
提问by Leonid
Some languages have the feature to return values using parameters also like C#. Let's take a look at an example:
某些语言也具有使用参数返回值的功能,例如 C#。我们来看一个例子:
class OutClass
{
static void OutMethod(out int age)
{
age = 26;
}
static void Main()
{
int value;
OutMethod(out value);
// value is now 26
}
}
So is there anything similar in Python to get a value using parameter, too?
那么在 Python 中是否有类似的东西也可以使用参数获取值?
采纳答案by Mark Tolonen
There is no reason to, since Python can return multiple values via a tuple:
没有理由,因为 Python 可以通过元组返回多个值:
def func():
return 1,2,3
a,b,c = func()
But you can also pass a mutable parameter, and return values via mutation of the object as well:
但是你也可以传递一个可变参数,并通过对象的变异返回值:
def func(a):
a.append(1)
a.append(2)
a.append(3)
L=[]
func(L)
print(L) # [1,2,3]
回答by extraneon
Pass a list or something like that and put the return value in there.
传递一个列表或类似的东西并将返回值放在那里。
回答by helloworld922
You mean like passing by reference?
你的意思是通过引用传递?
For Python object the default is to pass by reference. However, I don't think you can change the reference in Python (otherwise it won't affect the original object).
对于 Python 对象,默认是通过引用传递。但是,我认为您不能在 Python 中更改引用(否则它不会影响原始对象)。
For example:
例如:
def addToList(theList): # yes, the caller's list can be appended
theList.append(3)
theList.append(4)
def addToNewList(theList): # no, the caller's list cannot be reassigned
theList = list()
theList.append(5)
theList.append(6)
myList = list()
myList.append(1)
myList.append(2)
addToList(myList)
print(myList) # [1, 2, 3, 4]
addToNewList(myList)
print(myList) # [1, 2, 3, 4]
回答by s.m.
In addition, if you feel like reading some code, I think that pywin32has a way to handle output parameters.
另外,如果你喜欢阅读一些代码,我认为它pywin32有一种处理输出参数的方法。
In the Windows API it's common practice to rely heavily on output parameters, so I figure they must have dealt with it in some way.
在 Windows API 中,通常的做法是严重依赖输出参数,所以我认为他们必须以某种方式处理它。
回答by AndiDog
You can do that with mutable objects, but in most cases it does not make sense because you can return multiple values (or a dictionary if you want to change a function's return value without breaking existing calls to it).
您可以使用可变对象来做到这一点,但在大多数情况下,这是没有意义的,因为您可以返回多个值(如果您想在不中断现有调用的情况下更改函数的返回值,则可以返回字典)。
I can only think of one case where you might need it - that is threading, or more exactly, passing a value between threads.
我只能想到一种您可能需要它的情况——那就是线程,或者更准确地说,是在线程之间传递一个值。
def outer():
class ReturnValue:
val = None
ret = ReturnValue()
def t():
# ret = 5 won't work obviously because that will set
# the local name "ret" in the "t" function. But you
# can change the attributes of "ret":
ret.val = 5
threading.Thread(target = t).start()
# Later, you can get the return value out of "ret.val" in the outer function

