What does return do? Nothing is ever returned

后端 未结 4 2004
[愿得一人]
[愿得一人] 2020-12-04 04:06

I have been learning Python for some days now. However, I do not understand return. I have read several explanations from my textbooks and online; they don\'t help!

4条回答
  •  悲哀的现实
    2020-12-04 04:33

    The return keyword is to exit a function and return a value. To let a function return a value, use the return statement.

    As Charlie Duffy comments, when you don't use return anywhere in your function, Python has an implicit return value of None. In other words, if you don't tell a function it should return something, then None is what it returns.

    So if you change def gimmieFive(): return 5 to just def gimmieFive(): 5, then someone who runs x = gimmieFive() will have x == None, instead of x == 5. The 5 just gets thrown away when gimmieFive exits.

提交回复
热议问题