return,return None,根本不返回? [英] return, return None, and no return at all?

查看:22
本文介绍了return,return None,根本不返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑三个函数:

def my_func1():
  print "Hello World"
  return None

def my_func2():
  print "Hello World"
  return

def my_func3():
  print "Hello World"

它们似乎都返回 None.这些函数的返回值的行为方式有什么不同吗?是否有任何理由偏爱其中一种?

They all appear to return None. Are there any differences between how the returned value of these functions behave? Are there any reasons to prefer one versus the other?

推荐答案

在实际行为上,没有区别.他们都返回 None 就是这样.然而,所有这些都有时间和地点.以下说明基本上是应该如何使用不同的方法(或者至少是我被教导应该如何使用它们),但它们不是绝对规则,所以如果你觉得有必要可以将它们混合起来.

On the actual behavior, there is no difference. They all return None and that's it. However, there is a time and place for all of these. The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.

这表明该函数确实要返回一个值以供以后使用,在这种情况下它返回 None.这个值 None 然后可以在其他地方使用.如果函数没有其他可能的返回值,则永远不会使用 return None.

This tells that the function is indeed meant to return a value for later use, and in this case it returns None. This value None can then be used elsewhere. return None is never used if there are no other possible return values from the function.

在以下示例中,如果给出的 person 是人类,我们将返回 personmother.如果它不是人类,我们返回 None,因为 person 没有 mother(假设它不是动物或其他东西).

In the following example, we return person's mother if the person given is a human. If it's not a human, we return None since the person doesn't have a mother (let's suppose it's not an animal or something).

def get_mother(person):
    if is_human(person):
        return person.mother
    else:
        return None

使用 return

这与循环中的 break 的使用原因相同.返回值无关紧要,您只想退出整个函数.它在某些地方非常有用,即使您并不经常需要它.

Using return

This is used for the same reason as break in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.

我们有 15 个囚犯,我们知道其中一人拿着刀.我们一个一个地循环遍历每个prisoner,检查他们是否有刀.如果我们用刀打人,我们可以退出函数,因为我们知道只有一把刀,没有理由检查其余的prisoners.如果我们没有找到带刀的prisoner,我们会发出警报.这可以通过许多不同的方式完成,使用 return 可能甚至不是最好的方式,但这只是展示如何使用 return 退出函数的示例.

We've got 15 prisoners and we know one of them has a knife. We loop through each prisoner one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the prisoners. If we don't find the prisoner with a knife, we raise an alert. This could be done in many different ways and using return is probably not even the best way, but it's just an example to show how to use return for exiting a function.

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()

注意:你不应该这样做 var = find_prisoner_with_knife(),因为返回值并不意味着被捕获.

Note: You should never do var = find_prisoner_with_knife(), since the return value is not meant to be caught.

这也将返回 None,但该值并不打算被使用或捕获.它只是意味着函数成功结束.与 C++ 或 Java 等语言的 void 函数中的 return 基本相同.

This will also return None, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.

在下面的例子中,我们设置了人的母亲的名字,然后函数完成后退出.

In the following example, we set person's mother's name and then the function exits after completing successfully.

def set_mother(person, mother):
    if is_human(person):
        person.mother = mother

注意:你不应该这样做 var = set_mother(my_person, my_mother),因为返回值并不意味着被捕获.

Note: You should never do var = set_mother(my_person, my_mother), since the return value is not meant to be caught.

这篇关于return,return None,根本不返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆