函数返回多个值是pythonic吗? [英] Is it pythonic for a function to return multiple values?
问题描述
pre $ code def divide(x,y):
商= x / y
余额= x%y
返回商,余数
(q,r)=除(22,7)
这看起来很有用,但它看起来也可能被滥用(Well..function X已经计算了我们需要的中间值。让我们让X返回该值)。
何时应该画线并定义不同的方法?
绝对(您提供的示例)。
$ b
元组是Python中的第一类公民
有一个内建函数 divmod()
的确如此。 > q,r = divmod(x,y)#((x - x%y)/ y,x%y)不变:div * y + mod == x
还有其他的例子: zip
, enumerate code>,
dict.items
。
对于枚举([1,3,3])中的i,e:
printindex =%d ,元素=%s%(i,e)
#逆向键和字典中的值
d = dict((v,k)for k,v in adict.items() )#或
d = dict(zip(adict.values(),adict.keys()))
顺便说一下,大多数时候括号不是必需的。
引自 Python库参考资料:
元组由逗号
操作符(不在方括号内),
带或不带圆括号,
,但是一个空的元组必须有
括起括号,如a,b,c
或()。单个元组元组必须包含一个
尾随逗号,例如(d,)。
函数应该具有单一用途
因此他们应该返回一个单独的对象。在你的情况下,这个对象是一个元组。考虑元组作为专门的组合数据结构。有些语言几乎每一个函数都会返回多个值(Lisp中的列表)。
有时足够返回(x,y)
而不是 Point(x,y)
。
$ b
命名的元组
随着Python 2.6中命名元组的引入,在许多情况下最好返回命名元组而不是普通元组。
>>>导入集合
>>> Point = collections.namedtuple('Point','x y')
>>> x,y = Point(0,1)
>>> p = Point(x,y)
>>> x,y,p
(0,1,Point(x = 0,y = 1))
>>> p.x,p.y,p [0],p [1]
(0,1,0,1)
>>>对于我在p:
... print(i)
...
0
1
In python, you can have a function return multiple values. Here's a contrived example:
def divide(x, y):
quotient = x/y
remainder = x % y
return quotient, remainder
(q, r) = divide(22, 7)
This seems very useful, but it looks like it can also be abused ("Well..function X already computes what we need as an intermediate value. Let's have X return that value also").
When should you draw the line and define a different method?
Absolutely (for the example you provided).
Tuples are first class citizens in Python
There is a builtin function divmod()
that does exactly that.
q, r = divmod(x, y) # ((x - x%y)/y, x%y) Invariant: div*y + mod == x
There are other examples: zip
, enumerate
, dict.items
.
for i, e in enumerate([1, 3, 3]):
print "index=%d, element=%s" % (i, e)
# reverse keys and values in a dictionary
d = dict((v, k) for k, v in adict.items()) # or
d = dict(zip(adict.values(), adict.keys()))
BTW, parentheses are not necessary most of the time. Citation from Python Library Reference:
Tuples are constructed by the comma operator (not within square brackets), with or without enclosing parentheses, but an empty tuple must have the enclosing parentheses, such as a, b, c or (). A single item tuple must have a trailing comma, such as (d,).
Functions should serve single purpose
Therefore they should return a single object. In your case this object is a tuple. Consider tuple as an ad-hoc compound data structure. There are languages where almost every single function returns multiple values (list in Lisp).
Sometimes it is sufficient to return (x, y)
instead of Point(x, y)
.
Named tuples
With the introduction of named tuples in Python 2.6 it is preferable in many cases to return named tuples instead of plain tuples.
>>> import collections
>>> Point = collections.namedtuple('Point', 'x y')
>>> x, y = Point(0, 1)
>>> p = Point(x, y)
>>> x, y, p
(0, 1, Point(x=0, y=1))
>>> p.x, p.y, p[0], p[1]
(0, 1, 0, 1)
>>> for i in p:
... print(i)
...
0
1
这篇关于函数返回多个值是pythonic吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!