简体   繁体   English

是否可以在同一个 function 中获得不同的回报?

[英]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.

相关问题 如何为同一个功能使用不同的输入类型? - How to have different input types for the same function? 是否有可能有一个函数依赖于python中同一级别的另一个函数? - is it possible to have a function that depends on another function on the same level in python? 装饰器是否可以返回一个与包装函数具有相同参数的函数? - Is it possible for a decorator to return a function with the same parameters as the wrapped function? 普罗米修斯中是否可以使用不同标签的相同指标名称? - Is it possible to have same metric name with different labels in prometheus? kivy:是否可以在不同的屏幕上使用相同的选项卡式面板? - kivy: Is it possible to have the same Tabbed Panel on different screens? 两个图像是否有可能具有相同的像素值但仍然不同? - Is it possible for two images to have the same pixel values but still be different? 是否可以同时有一个 function output 一个生成器表达式和一个值 - Is it possible to have a function output a generator expression and a value at the same time 如何让 2 个不同的 function 并行运行,返回 Python 中的数组 - How to have 2 different function running parallel that return an array in Python Function 字节编码相同的输入并有不同的响应 - Function byte encode same input and have different responses 为什么两个不同对象上的哈希函数返回相同的值? - Why hash function on two different objects return same value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM