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'>
3 Answers
+ 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.
+ 1
Thanks! It is absolutely clear now. đ