Function returns None without return statement

后端 未结 7 2057
北荒
北荒 2020-11-21 06:19

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


        
7条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 06:49

    The problem is you wrote print jiskya(2,3). You're passing the return value of jiskya to the print function. jiskya itself prints x or y, which is why you see the 2. But then the print in the print jiskya(2, 3) statement itself executes with no argument.

    To the interpreter, this is a simplification of what happens:

    print jiskya(2,3)
    >> Executing jiskya with arguments 2, 3
    >> jiskya evaulates `print x`
    >> 2 is printed
    >> Function jiskya exits with no return value
    print None
    >> None is printed
    

提交回复
热议问题