简体   繁体   English

为什么在函数 python 中返回 None?

[英]why returning None in functions python?

first image I was having problem when I run this code I raise exception in function then why none is printing第一张图片我在运行此代码时遇到问题我在 function 中引发异常然后为什么没有打印

sec image And in this case none is not printing.. pls help sec image在这种情况下没有打印..请帮忙

when a python function does not have a return statement it automatically return None.当 python function 没有返回语句时,它会自动返回 None。

In the first image, the print(numcheck(5)) pass the assert but the function return None so it printed None but print(numcheck(-5)) did raise the exception.在第一张图片中, print(numcheck(5))传递了断言,但 function 返回 None,因此它打印了 None,但print(numcheck(-5))确实引发了异常。

In the second image print(numcheck(-5)) raise directly the exception so python did not print anything.在第二张图片中print(numcheck(-5))直接引发异常,所以 python 没有打印任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM