返回,返回无,根本没有返回?

问题:返回,返回无,根本没有返回?

考虑三个功能:

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

def my_func2():
  print "Hello World"
  return

def my_func3():
  print "Hello World"

它们似乎都返回None。这些函数的返回值的行为方式之间有什么区别吗?是否有任何理由偏爱一个?

Consider three functions:

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

def my_func2():
  print "Hello World"
  return

def my_func3():
  print "Hello World"

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?


回答 0

在实际行为上,没有区别。他们都回来了None,就是这样。但是,所有这些都有时间和地点。以下说明基本上是应如何使用不同方法的方法(或至少应告诉我应如何使用它们的方法),但是它们不是绝对规则,因此您可以根据需要将它们混合使用。

使用 return None

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

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

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

使用 return

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

我们已经有15个人,prisoners而且我们知道其中一个拥有一把刀。我们prisoner逐个循环检查他们是否有刀。如果我们用小刀打人,则可以退出该功能,因为我们知道只有一把小刀,没有理由检查其余部分prisoners。如果找不到prisoner刀子,则会发出警报。这可以通过许多不同的方式完成,使用return可能甚至不是最好的方式,但这只是说明如何使用return退出函数的一个示例。

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(),因为返回值不是要捕获的。

使用无return可言

这也将返回None,但是该值并不意味着要使用或捕获。这仅表示该功能已成功结束。它基本上与C ++或Java等语言return中的void函数相同。

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

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

注意:绝对不要这样做var = set_mother(my_person, my_mother),因为返回值不是要捕获的。

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.

Using 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.

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

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.

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()

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

Using no return at all

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

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


回答 1

是的,它们都是一样的。

我们可以查看解释后的机器代码,以确认它们都在做完全相同的事情。

import dis

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

def f2():
  print "Hello World"
  return

def f3():
  print "Hello World"

dis.dis(f1)
    4   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    5   5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f2)
    9   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    10  5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f3)
    14  0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE            
        5 LOAD_CONST    0 (None)
        8 RETURN_VALUE      

Yes, they are all the same.

We can review the interpreted machine code to confirm that that they’re all doing the exact same thing.

import dis

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

def f2():
  print "Hello World"
  return

def f3():
  print "Hello World"

dis.dis(f1)
    4   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    5   5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f2)
    9   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    10  5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f3)
    14  0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE            
        5 LOAD_CONST    0 (None)
        8 RETURN_VALUE      

回答 2

它们每个都返回相同的单例None-功能上没有差异。

我认为,return除非您需要先退出该函数(在这种情况下,裸露return更为常见)或返回除以外的其他值,否则放弃该语句是很习惯的做法None。它也很有意义,并且return None在函数中具有返回除之外的其他值的函数时似乎是惯用的Nonereturn None明确地写出来是读者的视觉提示,还有另一个分支返回更有趣的内容(并且调用代码可能需要处理两种类型的返回值)。

返回的函数通常在Python None中像voidC中的函数一样使用-它们的目的通常是在适当的位置对输入参数进行操作(除非您正在使用全局数据(shudders))。None通常,返回可以使参数更加明确。这使我们更加清楚为什么return从“语言约定”的角度出发不理会该声明。

就是说,如果您正在使用已经针对这些事情设置了预设约定的代码库,那么我肯定会效仿以帮助使代码库保持一致…

They each return the same singleton None — There is no functional difference.

I think that it is reasonably idiomatic to leave off the return statement unless you need it to break out of the function early (in which case a bare return is more common), or return something other than None. It also makes sense and seems to be idiomatic to write return None when it is in a function that has another path that returns something other than None. Writing return None out explicitly is a visual cue to the reader that there’s another branch which returns something more interesting (and that calling code will probably need to handle both types of return values).

Often in Python, functions which return None are used like void functions in C — Their purpose is generally to operate on the input arguments in place (unless you’re using global data (shudders)). Returning None usually makes it more explicit that the arguments were mutated. This makes it a little more clear why it makes sense to leave off the return statement from a “language conventions” standpoint.

That said, if you’re working in a code base that already has pre-set conventions around these things, I’d definitely follow suit to help the code base stay uniform…


回答 3

正如其他人回答的那样,None在所有情况下都将返回完全相同的结果。

区别是风格上的,但请注意,PEP8要求使用时要保持一致:

在返回语句中保持一致。函数中的所有return语句应返回一个表达式,或者都不返回。如果任何return语句返回一个表达式,则不返回任何值的任何return语句应将其显式声明为return None,并且在函数的末尾(如果可访问)应存在显式return语句。

是:

def foo(x):
    if x >= 0:
        return math.sqrt(x)
    else:
        return None

def bar(x):
    if x < 0:
        return None
    return math.sqrt(x)

没有:

def foo(x):
    if x >= 0:
        return math.sqrt(x)

def bar(x):
    if x < 0:
        return
    return math.sqrt(x)

https://www.python.org/dev/peps/pep-0008/#programming-recommendations


基本上,如果您曾经非None在函数中值,则意味着返回值具有含义,并且被调用方捕获。因此,当您返回时None,它也必须是显式的,以None在这种情况下传达含义,它是可能的返回值之一。

如果您根本不需要返回,则函数基本上是作为过程而不是函数工作的,因此不要包括 return语句。

如果您正在编写类似过程的函数,并且有机会早点返回(即您已经完成了此操作,不需要执行其余的函数),则可以使用empty returns向读者发出信号这只是执行的早期完成,None隐式返回的值没有任何意义,也不意味着被捕获(类似过程的函数始终返回None)。

As other have answered, the result is exactly the same, None is returned in all cases.

The difference is stylistic, but please note that PEP8 requires the use to be consistent:

Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable).

Yes:

def foo(x):
    if x >= 0:
        return math.sqrt(x)
    else:
        return None

def bar(x):
    if x < 0:
        return None
    return math.sqrt(x)

No:

def foo(x):
    if x >= 0:
        return math.sqrt(x)

def bar(x):
    if x < 0:
        return
    return math.sqrt(x)

https://www.python.org/dev/peps/pep-0008/#programming-recommendations


Basically, if you ever return non-None value in a function, it means the return value has meaning and is meant to be caught by callers. So when you return None, it must also be explicit, to convey None in this case has meaning, it is one of the possible return values.

If you don’t need return at all, you function basically works as a procedure instead of a function, so just don’t include the return statement.

If you are writing a procedure-like function and there is an opportunity to return earlier (i.e. you are already done at that point and don’t need to execute the remaining of the function) you may use empty an returns to signal for the reader it is just an early finish of execution and the None value returned implicitly doesn’t have any meaning and is not meant to be caught (the procedure-like function always returns None anyway).


回答 4

就功能而言,它们都是相同的,它们之间的区别在于代码的可读性和样式(要考虑的重要因素)

In terms of functionality these are all the same, the difference between them is in code readability and style (which is important to consider)