0

Query on python type method

During a challenge came across the question: What is the output of print (type (print ("9")))? The output is : 9 <class 'NoneType'> Why class is displayed as NoneType? On the other hand, print (type (print)) returns <class 'builtin_function_or_method'>

3rd Dec 2019, 7:45 PM
Rini
Rini - avatar
3 odpowiedzi
+ 1
Let's break it into pieces in order to understand. print(type (print ("9"))) print ("9") is a call to the print function which returns nothing when called. In python when a function returns nothing it returns None by default. print (type (None)) type (None) will return the NoneType and then it will be printed. In the second case print(type (print)) In type (print), print is not a function call. It is the function object itself. So when you get its type it will be builtin_function_or_method. Then the outer print prints that type.
3rd Dec 2019, 7:57 PM
Amr Saeed
Amr Saeed - avatar
+ 1
Thanks! It is absolutely clear now. 👍
3rd Dec 2019, 8:14 PM
Rini
Rini - avatar