[英]Is it possible to have different return in the same function?
I have that function:我有 function:
def choice(default=True):
if default:
return 1
else:
return 0, "Your choice is 0"
In the first case I get one variable, in the second case I get two variables.在第一种情况下我得到一个变量,在第二种情况下我得到两个变量。
Is it a problem to do that in Python?在 Python 中这样做有问题吗? I tested and it works, but for instance in c++ that kind of things does not work.
我测试过并且它有效,但例如在 c++ 中,这种事情不起作用。 Do you know if there is a rule about that such as PEP8 or something like that?
你知道是否有关于 PEP8 之类的规则吗?
Thank you very much !非常感谢 !
It's absolutely fine to have a function with different return value signatures, depending on the options passed.根据传递的选项,具有不同返回值签名的 function 绝对没问题。 See
np.unique
for example . 例如,参见
np.unique
。 Since Python does not have function overloading, this is the canonical way of achieving that.由于 Python 没有 function 重载,这是实现它的规范方法。 The important point is to document it properly.
重要的一点是正确记录它。 In the example of
np.unique
you'd otherwise have to define eight different versions of the function with all combinations of return values.在
np.unique
的示例中,否则您必须定义八个不同版本的 function 以及返回值的所有组合。 Impractical, hard to maintain, and mostly redundant.不切实际,难以维护,而且大多是多余的。
Another way to handle this kind of "function overloading" in Python is to always use the same return value signature, but return None
for unused values. Python 中处理这种“函数重载”的另一种方法是始终使用相同的返回值签名,但对未使用的值返回
None
。 I like the former method better.我更喜欢前一种方法。
In the first case I get one variable, in the second case I get two variables.
在第一种情况下我得到一个变量,在第二种情况下我得到两个变量。
Actually in both cases you get a single object. In the first case it is an int, and in the second case it is a tuple (with int and string inside).实际上,在这两种情况下,您都会得到一个 object。在第一种情况下,它是一个 int,在第二种情况下,它是一个元组(内部包含 int 和 string)。
Is it a problem to do that in Python?
在 Python 中这样做有问题吗?
Technically?技术上? No. In terms of maintenance and readability?
不。在维护和可读性方面? Yes.
是的。 Can you imagine someone's surprise when he realizes that changing an argument changes the type of the result (in your case int vs tuple)?
当某人意识到改变参数会改变结果的类型(在你的例子中是 int vs 元组)时,你能想象他的惊讶吗? What's worse these types are completely unrelated.
更糟糕的是,这些类型完全不相关。 Typically noone will expect that.
通常没有人会想到这一点。 And then they have to write weird logic to handle it.
然后他们必须编写奇怪的逻辑来处理它。 Annoying.
恼人的。
Also how would you document this function?另外,您将如何记录此 function? The return type is inconsistent, unless you simply say "it returns an object" which is too general to be useful.
返回类型不一致,除非你简单地说“它返回一个对象”,这太笼统了,没有用。
Finally such design will likely confuse any static code analysis.最后这样的设计很可能会混淆任何 static 代码分析。 And we want static analysis to reduce human errors.
并且我们希望 static 分析以减少人为错误。
I tested and it works, but for instance in c++ that kind of things does not work.
我测试过并且它有效,但例如在 c++ 中,这种事情不起作用。
It does work in C++ as well (and probably in any language), although in a bit different way.它也适用于 C++(并且可能适用于任何语言),尽管方式有所不同。 After all what Python returns is "an object", it is up to the caller to interpret that.
毕竟 Python 返回的是“一个对象”,由调用者来解释它。 It is very similar to return
void*
from a C++ function and force the caller to do appropriate cast.它非常类似于从 C++ function 返回
void*
并强制调用者进行适当的转换。 Which is a very bad practice as well.这也是一种非常糟糕的做法。 Even worse in C++, since it is strongly typed.
C++ 更糟,因为它是强类型的。
Do you know if there is a rule about that such as PEP8 or something like that?
你知道是否有关于 PEP8 之类的规则吗?
Well, not explicitly about this situation, but "PEP 8 -- Style Guide for Python Code" does say "Be consistent in return statements.".好吧,没有明确说明这种情况,但是“PEP 8——Python 代码的样式指南”确实说“在返回语句中保持一致。”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.