在Python中是否可以使返回语句依赖于实例的类型?

huangapple go评论65阅读模式
英文:

Is it possible to make return statement dependent on the type of instance in python?

问题

以下是要翻译的代码部分:

def do_something():
    some_list = [1, 4, 7, 8]
    some_text = "Hello"
    return some_text or some_list


text: str = do_something()  # 希望text变成some_text(即"Hello")
List: list[int] = do_something()  # List应该取some_list的值(即[1, 4, 7, 8])
print(text, List)  # 希望它打印< Hello [1, 4, 7, 8] >

希望这对你有所帮助。如果你有任何其他问题,请随时提问。

英文:

It is hard to formulate a question… I want to create a function that can return for example string OR list. It will return depends on the type of instance this value is assigned to latter. For example, consider the following code attempt:

def do_something():
    some_list = [1, 4, 7, 8]
    some_text = &quot;Hello&quot;
    return some_text or some_list


text: str = do_something()  # want text to become some_text (i.e. &quot;Hello&quot;)
List: list[int] = do_something()  # List should take some_list value (so [1, 4, 7, 8])
print(text, List)  # want it to print &lt; Hello [1, 4, 7, 8] &gt;

I know, that the following code will not work for obvious reasons. I also know that I can return a list or a tuple in return statement, like return some_text, some_list. But then I will need to state, which value to take and I really don't want to make something like text: str = do_something()[0].

That is because I just want to create a function that will return main value (function main task) and some additional that is less important and fully optional. Using tuple for me means that they are both equally important, but they are not, by default return always should return the first value. Can I do that in python, or is it completely impossible?

答案1

得分: 1

这是不可能的。

你最好的选择可能是要么创建一个第二个函数来返回其他数据类型,要么将你想要的内容传递给函数:

def do_something(as_list=False):
    some_list = [1, 4, 7, 8]
    some_text = "Hello"
    if as_list:
        return some_list
    else:
        return some_text

text: str = do_something()  # 希望text变成some_text(即"Hello")
List: list[int] = do_something(as_list=True)  # List应该取some_list的值(即[1, 4, 7, 8])
print(text, List)  # 希望它打印出 < Hello [1, 4, 7, 8] >

是否创建不同的函数或将其作为参数传递,主要取决于你的业务逻辑和需求。

英文:

This is not possible.

Your best call would probably be to either create a second function to return the other data type or to pass what you want to the function:

def do_something(as_list = False):
    some_list = [1, 4, 7, 8]
    some_text = &quot;Hello&quot;
    if as_list:
        return some_list
    else:
        return some_text    

text: str = do_something()  # want text to become some_text (i.e. &quot;Hello&quot;)
List: list[int] = do_something(as_list=True)  # List should take some_list value (so [1, 4, 7, 8])
print(text, List)  # want it to print &lt; Hello [1, 4, 7, 8] &gt;

Whether you create a different function or pass it as an argument mostly depends on your business logic and needs.

答案2

得分: 1

你可以尝试这样的代码:

def do_something(var):
    some_list = [1, 4, 7, 8]
    some_text = "Hello"
    if type(var) == str:
        return some_text
    elif type(var) == list:
        return some_list
    else:
        return None

text = ""
text = do_something(text)

lst = []
lst = do_something(lst)

print(text, lst)

这将产生以下结果:

Hello [1, 4, 7, 8]
英文:

You could go for something like this:

def do_something(var):
    some_list = [1, 4, 7, 8]
    some_text = &quot;Hello&quot;
    if type(var) == str:
        return some_text
    elif type(var) == list:
        return some_list
    else:
        return None

text = &quot;&quot;
text = do_something(text)

lst = []
lst = do_something(lst)

print(text, lst)

Which results in

Hello [1, 4, 7, 8]

huangapple
  • 本文由 发表于 2023年8月4日 06:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定