Function returns None without return statement

后端 未结 7 1828
北荒
北荒 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:47

    Consider following examples:

    Function without return statement

    Print() function type is none type..

    def test1():
    
        print("code...!!!")
    
    type(test1())
    
    Output: code...!!!
            NoneType
    

    Function with return statement

    Returning variable 'a' which holds print() function, that's why type() returns data type of print function which is NoneType, not the data type of what's inside the print funcion:

    def test1():
    
        a= print("code...!!!")
        
        return a
    
    type(test1())
    
    
    Output: code...!!!
            NoneType
    

    Here function returning data type of variable 'a' which holds a string in it.

    def test1():
    
        a = "First code...!!!"
        
        return a
    
    type(test1())
    
    
    Output: str
    
    

提交回复
热议问题