I. Wewib
I. Wewib

Reputation: 363

Funny/Weird Mechanics of the print() Function

I am in the process of learning python. I am currently learning how to define your own functions and I had some difficulty trying to understand the difference between returning a value and printing it. The descriptions that I have read about the subject were not so clear to me, so I started experimenting on my own and I think I got it by now. If I am not wrong, the difference is that you can pass the value of a return expression as an argument to another function and you cannot do that with the value of a print() function. While experimenting, I encountered some unexpected outputs.

The first thing I did was write the following code in the Python Interpreter:

>>>print(print('hello'))

The output was:

hello

None

This made me believe the following: the print('hello') function gets executed first and the text hello shows up. Then the expression print(print('hello)) evaluates to None since you cannot use a print() function as an argument of another function. So far so good.

The next code I entered was:

>>>a = print('hello')

You normally do not expect an output when assigning something to a variable, but in this case the output was:

hello

This is what I think: the print('hello') expression gets executed and outputs the text hello. Then the print('hello') expression gets stored in the variable a.

So this led me to the last line of code I entered:

>>>print(a)

Based on the previous outputs, I expected this code to give the following output:

hello

None

Because the variable a will get replaced with print('hello') and then you will get the same expression as the first one, i.e.: print(print('hello')).

But this was not the case. The actual output was just:

None

Now I have a couple of unanswered questions:

  1. Why is the output of the last code just None?
  2. If you cannot put a print() function inside another print() function, why is the output None? Why does it not give you an error message?
  3. Are any of my assumptions wrong (including the one about the difference between return and print)?

I know that I made this post way too long, but I like to give the whole context so possible helpers will know my situation and maybe adjust their way of helping to fit the situation the best.

Thanks in advance!

Upvotes: 0

Views: 455

Answers (1)

freakish
freakish

Reputation: 56557

Then the print('hello') expression gets stored in the variable a

No, the variable a does not hold the expression. Generally you cannot assign an expression to a variable. What actually happens is that you assign the result of the expression to the variable. Subtle difference but very important.

Every function in Python returns something. If you write

>>> a = fn()

then you store in a the result of calling fn, not the expression fn(). If for example

def fn():
    return 1

then a is now 1. It just happens that print function returns None.

On the other hand print function has a side effect. The side effect is that it pushes whatever you pass to it to the standard output (if you don't mess with sys then to sys.stdout). It "prints".

So lets analyze your example:

>>> a = print('hello')

What happens is: print('hello') is called. The side effect fires: hello is send to the screen. Then the result of calling print is stored in a which is None. Doing

>>> print(a)

simply prints None because that's what a is now.

Upvotes: 7

Related Questions