没有返回语句的函数返回 None [英] Function returns None without return statement

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

问题描述

我刚刚学习(正在学习)函数参数在 Python 中的工作原理,并且我开始毫无理由地尝试它,当时:

def jiskya(x, y):如果 x >y:打印(y)别的:打印(x)打印(jiskya(2, 3))

给出输出:

<预><代码>>>>2没有任何

None 从何而来?那是什么?

解决方案

是函数的返回值,你打印出来.如果没有 return 语句(或 只是一个没有参数的 return),一个隐式的return None被添加到函数的末尾.

您可能希望在函数中返回值而不是打印它们:

def jiskya(x, y):如果 x >y:返回 y别的:返回 x打印(jiskya(2, 3))

I just learned (am learning) how function parameters work in Python, and I started experimenting with it for no apparent reason, when this:

def jiskya(x, y):
    if x > y:
        print(y) 
    else:
        print(x) 

print(jiskya(2, 3))

gave the ouput:

>>>
2
None

Where did the None come from? And what is it?

解决方案

It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.

You probably want to return the values in the function instead of printing them:

def jiskya(x, y):
    if x > y:
        return y
    else:
        return x

print(jiskya(2, 3))

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

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