返回,返回无,根本没有返回? [英] return, return None, and no return at all?

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

问题描述

考虑三个功能:

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 以检查他们是否有刀.如果我们用刀击中了人,我们可以退出函数,因为我们知道只有一把刀并且没有理由检查其余的囚犯.如果我们没有用刀找到囚犯,我们就会发出警报.这可以通过许多不同的方式完成,使用 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.

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

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