Python如何从函数返回多个值? [英] How does Python return multiple values from a function?
问题描述
class FigureOut:
def setName(self,name):
fullname = name.split()
self.first_name =全名[0]
self.last_name =全名[1]
get getName(self):
return self.first_name,self.last_name
$ bf = FigureOut()
f.setName(Allen Solly)
name = f.getName()
打印(名称)
我收到以下输出:
$ p $
('Allen','Solly')
每当有多个值从python函数返回时,它是否总是将多个值转换为多个值列表,然后从函数中返回它?
整个过程与将多个值明确转换为 list
然后返回列表相同在JAVA中,只能从JAVA函数返回一个对象? getName 中的return语句指定了 多元素:
def getName(self):
返回self.first_name,self.last_name
Python将返回一个基本上包含它们的容器对象。
在这种情况下,返回逗号分隔元素集创建元组。多个值只能在容器中返回。
让我们使用一个更简单的函数返回多个值:
def foo(a,b):
返回a,b
您可以查看使用 dis.dis
,这是Python字节码的反汇编程序。对于不包含任何括号的逗号分隔值,它看起来像这样:
>>> import dis
>>> def foo(a,b):
... return a,b
>>> (a)
3 LOAD_FAST 1(b)
6 BUILD_TUPLE 2
9 RETURN_VALUE
正如你所看到的,这些值首先在内部堆栈上加载,并且 LOAD_FAST
然后一个 BUILD_TUPLE
(抓取放置在堆栈上的以前的 2
元素)。由于存在逗号,Python知道要创建一个元组。
您也可以使用指定另一个返回类型,例如列表, ]
。在这种情况下, BUILD_LIST $ c $
>>>将会按照相同的语义进行发布。 def foo_list(a,b):
... return [a,b]
>>> (b)
6 BUILD_LIST 2
9 RETURN_VALUE
$ / code >
返回的对象类型实际上取决于括号的存在(对于元组()
可以如果至少有一个逗号,则省略)。 ()
创建元组,
>>> first_name,last_name = f.getName()
>>> print(first_name,last_name)
所有这些,你的Java方式都渗透到Python中:-)
不要在使用Python编写类时使用getters,使用 properties
。属性是管理属性的惯用方式,更多关于这些的信息,请参阅一个很好的答案 here 。
I have written the following code:
class FigureOut:
def setName(self, name):
fullname = name.split()
self.first_name = fullname[0]
self.last_name = fullname[1]
def getName(self):
return self.first_name, self.last_name
f = FigureOut()
f.setName("Allen Solly")
name = f.getName()
print (name)
I get the following Output:
('Allen', 'Solly')
Whenever multiple values are returned from a function in python, does it always convert the multiple values to a list of multiple values and then returns it from the function?
Is the whole process same as converting the multiple values to a list
explicitly and then returning the list, for example in JAVA, as one can return only one object from a function in JAVA?
Since the return statement in getName
specifies multiple elements:
def getName(self):
return self.first_name, self.last_name
Python will return a container object that basically contains them.
In this case, returning a comma separated set of elements creates a tuple. Multiple values can only be returned inside containers.
Let's use a simpler function that returns multiple values:
def foo(a, b):
return a, b
You can look at the byte code generated by using dis.dis
, a disassembler for Python bytecode. For comma separated values w/o any brackets, it looks like this:
>>> import dis
>>> def foo(a, b):
... return a,b
>>> dis.dis(foo)
2 0 LOAD_FAST 0 (a)
3 LOAD_FAST 1 (b)
6 BUILD_TUPLE 2
9 RETURN_VALUE
As you can see the values are first loaded on the internal stack with LOAD_FAST
and then a BUILD_TUPLE
(grabbing the previous 2
elements placed on the stack) is generated. Python knows to create a tuple due to the commas being present.
You could alternatively specify another return type, for example a list, by using []
. For this case, a BUILD_LIST
is going to be issued following the same semantics as it's tuple equivalent:
>>> def foo_list(a, b):
... return [a, b]
>>> dis.dis(foo_list)
2 0 LOAD_FAST 0 (a)
3 LOAD_FAST 1 (b)
6 BUILD_LIST 2
9 RETURN_VALUE
The type of object returned really depends on the presence of brackets (for tuples ()
can be omitted if there's at least one comma). ()
creates tuples, []
lists, {}
sets.
To summarize, one actual object is returned. If that object is of a container type, it can contain multiple values giving the impression of multiple results returned. The usual method then is to unpack them directly:
>>> first_name, last_name = f.getName()
>>> print (first_name, last_name)
As an aside to all this, your Java ways are leaking into Python :-)
Don't use getters when writing classes in Python, use properties
. Properties are the idiomatic way to manage attributes, for more on these, see a nice answer here.
这篇关于Python如何从函数返回多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!