Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

python - Should a return statement have parentheses?

Suppose we have in Python 3.x (and I guess in Python 2.6 and in Python 2.7 too) the following functions:

>>> def dbl_a(p): return p*2
>>> def dbl_b(p): return(p*2)
>>> def dbl_c(p): return (p*2)

If we run them we get:

>>> dbl_a(42)
84
>>> dbl_b(42)
84
>>> dbl_c(42)
84

The three functions provide the same result (value and type) and they seem to be equivalent.

But which of them has the more correct return statement?

Is there any side-effect in any of those definitions?

The same questions apply to the following situation with multiple values returned:

>>> def dbl_triple_a(p): return p*2, p*3
>>> def dbl_triple_b(p): return(p*2, p*3)
>>> def dbl_triple_c(p): return (p*2, p*3)

>>> dbl_triple_a(42)
(84, 126)
>>> dbl_triple_b(42)
(84, 126)
>>> dbl_triple_c(42)
(84, 126)

In this case every function returns a tuple, but my questions still remain the same.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

return value is the "correct" way - return is a language construct, not a function.

If you want to return a tuple, use return your, values, here

There's no need for any parenthesis (tuples are created by the , "operator", not the ())


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...