-2

After running the following code. I observed that the return type of a print() is None. Please explain why and how is it so?

>>> v = print(7)                                                                                                                     >>> v                                                                                                                   >>> type(v)                                                                                                             <class 'NoneType'>                                                                                                      >>>  
>>> type(print(y))
7
<class 'NoneType'>
>>> type(None)
<class 'NoneType'>
Agnij Moitra
  • 87
  • 1
  • 11
  • `type(print)` returns type of function, `type(print())` returns type of value returned from function – Olvin Roght May 09 '21 at 16:25
  • 3
    You didn't declare `v` as a function, you declared it to be the *return value* of a particular function call (which returned `None`). If you did `v = print` or maybe `v = lambda: print(y)` then `v` would be a function and your `type` call would show ``. – Samwise May 09 '21 at 16:26
  • 4
    The part where you typed `>>> v` and got the output `7` would not really happen. It would give no output. – interjay May 09 '21 at 16:27
  • Now I've corrected the typos – Agnij Moitra May 18 '21 at 12:24

2 Answers2

1

You need to use:

type(print)

if you add the parenthesis it calls the function and type() will give you the type of the return value

Eno Gerguri
  • 639
  • 5
  • 22
0

If a function does not return anything, it returns None in python.

You are confusing print with return. The job of print function is to just convert value to string and print, it doesn't return anything, so it is 'NoneType'.

You can refer to this for further detailed explanation.