python的return

Posted Splace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python的return相关的知识,希望对你有一定的参考价值。

关于python的return用法,在stackoverflow里的问题:

Python — return, return None, and no return at all

Consider three functions:

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

def my_func2():
  print "Hello World"
  return

def my_func3():
  print "Hello World"

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 so).

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

Note: You should never do var = get_mother(), since let us assume var is a dictionary and you iterate by var.items() it will give an error citing None has no method items(). It you intentionally return None then it must be taken care of in the calling function appropriately.

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 tho you don‘t need it that often.

We 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 cause 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 the 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.


以上是关于python的return的主要内容,如果未能解决你的问题,请参考以下文章

Python代码阅读(第25篇):将多行字符串拆分成列表

常用python日期日志获取内容循环的代码片段

javascript常用代码片段

python 有用的Python代码片段

js常用代码片段(更新中)

Python 函数声明和调用