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中 return 的用法
return 语句就是讲结果返回到调用的地方,并把程序的控制权一起返回 程序运行到所遇到的第一个return即返回(退出def块),不会再运行第二个return. 要返回两个数值,写成一行即可: de ...
- Python中return self的用法
在Python中,有些开源项目中的方法返回结果为self. 对于不熟悉这种用法的读者来说,这无疑使人困扰,本文的目的就是给出这种语法的一个解释,并且给出几个例子. 在Python中,retur ...
- python中return和print的区别(详细)
huskiesir最近在研究python哈,今天纠结一个问题,那就是return和print的区别,都是可以输出结果的,到底有啥区别呀?二话不多说,看下面的例子. #代码1: def break_wo ...
- python——比return优先级更高的语句
调用sqlmap,使用sqlmap做二次开发的时候,出现的问题: 在调用sqlmap中return,然而主程序还是会被sqlmap中的某些代码给中断. 添加try也无法阻止中断. 后来猜测中断是由ex ...
- python的return self的用法
转载:https://blog.csdn.net/jclian91/article/details/81238782 class foo: def __init__(self): self.m = 0 ...
- [Python]关于return逻辑判断和短路逻辑
定义一个return...and..函数: def res(): ' 返回结果: >>> print(res()) 1234 定义一个return...or..函数: def res ...
- python中return的用法
def testReturn(x): if x > 10000: return print "test return!!" #这句话永远不会得到执行 elif x > ...
- Python的return self和return一个新的对象区别
目的:设计一个有理数相加.如3/5 + 7/15 = 80/75 return self 输入: class Rational0: def __init__(self, num, den=1): se ...
- Python随笔---return与print,全局变量与局部变量
Return是指返回一个数值,一般在函数中应用较多 Print则是指把结果打印出来,显示在屏幕上 def sum(a,b): total=a+b print('函数内:',total) return ...
随机推荐
- prisma middleware 简化 graphql resolver 编写的类库
prisma 推出middleware 的目的就是保持resolver 的简洁 作用: 输入参数访问同一个resolver 决定resolver 最终的返回值 在resolver 连中捕获异常以及 ...
- Asp.net MVC 出现“Failed to map the path '/'.”错误
Server Error in '/' Application. Parser Error Description: An error occurred during the parsing of a ...
- PHP实现IMAP
//以腾讯企业邮箱做了测试 $mailServer="imap.exmail.qq.com"; //IMAP主机 $mailLink="{{$mailServer}:14 ...
- Mysql8.0 Public Key Retrieval is not allow错误的解决办法
在使用Mysql 8.0时重启后启动项目的事后会报错com.mysql.jdbc.exceptions.jdbc4.MysqlNonTransientConnectionException: Publ ...
- Mac上如何把图片中的文字转换成word/pdf文字
如何把图片文字转换成word文字? - 知乎 https://www.zhihu.com/question/25488536 在 OneNote for Mac 中插入的圖片複製文字 - OneNot ...
- windows8.1中组件服务DCOM配置里属性灰色不可修改的解决办法
由于电脑升级,更换成了windows8.1的64位操作系统,今天遇到组件服务中DCOM配置里IIS Admin Service属性呈现灰色,不能修改. 查找官方文档,原来这是win8.1 x64的安全 ...
- python logging 重复写日志问题
用Python的logging模块记录日志时,遇到了重复记录日志的问题,第一条记录写一次,第二条记录写两次,第三条记录写三次...很头疼,这样记日志可不行.网上搜索到了原因与解决方案: 原因:没有移除 ...
- ggplot map
ggplot {ggplot2} R Documentation Create a new ggplot Description ggplot() initializes a ggplot objec ...
- Rhythmk 一步一步学 JAVA (18): Enum枚举学习
枚举定义: public enum SizeEnum { SMALL, BIG, BIGEST }; public enum SizeStringEnum { SMALL("小") ...
- Spring Boot实践——Spring AOP实现之动态代理
Spring AOP 介绍 AOP的介绍可以查看 Spring Boot实践——AOP实现 与AspectJ的静态代理不同,Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改 ...