返回元组或 None 的函数:如何很好地调用该函数?
假设如下:
def MyFunc(a):
if a < 0:
return None
return (a+1, a+2, a+3)
v1, v2, v3 = MyFunc()
# Bad ofcourse, if the result was None
定义一个返回元组并且可以很好地调用的函数的最佳方法是什么。目前,我可以这样做:
r = MyFunc()
if r:
v1, v2, v3 = r
else:
# bad!!
pass
我不喜欢的是我必须使用单个变量然后将其解压。
另一个解决方案是我可以让函数返回一个充满 None 的元组,以便调用者可以很好地解压......
任何人都可以提出更好的设计吗?
Suppose the following:
def MyFunc(a):
if a < 0:
return None
return (a+1, a+2, a+3)
v1, v2, v3 = MyFunc()
# Bad ofcourse, if the result was None
What is the best way to define a function that returns a tuple and yet can be nicely called. Currently, I could do this:
r = MyFunc()
if r:
v1, v2, v3 = r
else:
# bad!!
pass
What I don't like about this is that I have to use a single variable and then unpack it.
Another solution is I could have the function return a tuple full of Nones so that the caller can nicely unpack....
Anyone can suggest a better design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
引发一个
ArgumentError
怎么样?然后您可以尝试
调用它,并在参数错误时处理异常。所以,类似:
另外,请参阅 katrielalex 对于使用 ArgumentError 子类的回答。
How about raise an
ArgumentError
? Then you couldtry
calling it, and deal with the exception if the argument is wrong.So, something like:
Also, see katrielalex's answer for using a subclass of ArgumentError.
这应该可以很好地工作:
当
MyFunc()
返回一个元组时,它将被解包,否则它将被替换为None
的3元组。This should work nicely:
When
MyFunc()
returns a tuple, it will be unpacked, otherwise it will be substituted for a 3-tuple ofNone
.recursive
有一个真正优雅且Pythonic 的解决方案。但是:为什么要返回None
? Python 有一种处理错误的方法,那就是引发异常:然后
这种方法更好的原因是返回一个值表明您已完成处理并正在传回您的答案。如果您已经完成了一些处理,这很好,但如果您立即返回
None
,那就很愚蠢了。recursive
has a truly elegant and Pythonic solution. BUT: why do you want to returnNone
? Python has a way of handling errors, and that is by raising an exception:and then
The reason this is better is that returning a value indicates that you have completed processing and are passing back your answer. This is fine if you have done some processing, but it's silly if you are immediately returning
None
.这有什么问题?一致性是一件好事。
What's wrong with that? Consistency is a good thing.
如果您希望
v1、v2、v3
对象存在并在发生错误时设置为默认值,请自行返回默认值。这将使调用代码更简单,不依赖调用者手动设置它们:另一方面,如果默认返回不合适并且负参数被认为是严重错误,则引发异常:
在第三个 很难,当返回单个对象时,有时返回
None
可能更好,就像search()
和match()re
模块的 code> 函数。它在某种程度上介于前两种情况之间,因为匹配失败是预期结果,而默认返回对象无论如何都不是很有用。If you want the
v1, v2, v3
objects to exist and be set to a default value in the case of an error, return the default values yourself. This will make the calling code simpler by not relying on the caller to set them manually:On the other hand, if a default return is not appropriate and a negative argument is considered a serious error, raise an exception:
On the third hard, returning
None
may be preferable sometimes when returning a single object, as is the case with thesearch()
andmatch()
functions of there
module. It somehow stands between the first two cases, because matching failure is an expected outcome, while a default return object wouldn't be very useful anyway.这与之前的答案类似。您可以返回一个对象实例或 None
This is similar to the previous answer. You could return either an object instance or None