Ex18 - Functions

print(">>> print_two=", repr(print_two("Test","Testing")))
print_two("Zed","Shaw")
print(">>> print_two_again=", repr(print_two_again("Test","Testing")))
print_two_again("Zed","Shaw")
print(">>> print_one=", repr(print_one("One!")))
print_one("First!")
print(">>> print_none=", repr(print_none()))
print_none()
arg1: Test, arg2: Testing
>>> print_two= None
arg1: Zed, arg2: Shaw
arg1: Test, arg2: Testing
>>> print_two_again= None
arg1: Zed, arg2: Shaw
arg1: One!
>>> print_one= None
arg1: First!
I got nothin'.
>>> print_none= None
I got nothin'.

I don’t understand why the repr() output is None. Is it calling the function separately within repr so the output is none?

Why do you use repr to begin with?

I presum print_two doesn’t have a return statement?

In that case here’s what happens when Python executes your first line:

Python says: Ok, I should print something. What should I print? Well, at first ">>> print_two=", which is a string; and then there’s this thing which I don’t have a clue about yet:

repr(print_two("Test", "Testing"))

Python executes the outer function call, repr. repr takes something and generates a printable representation of it. Of what? Well, that’s print_two("Test", "Testing"), another function call. So Python goes on and executes this inner function call.

print_two is called, and it has a side effect: It prints something on the screen. That’s the first line of your output. But as far as repr is concerned, Python doesn’t care about any side effects, it needs the return value of print_two to make repr generate something printable of it. Now print_two doesn’t have a return statement, so it’s evaluated to None and that’s what is handed back to repr. Then repr(None) is evaluated and that’s simply "None".

Now finally print knows what to print: Two strings, ">>> print_two=" and "None", and that’s the second line of your output.

Does this help?

1 Like

I was trying to do the debugging print statements that Zed has shown in the videos to try and understand how it worked. I thought I could use repr but it doesn’t make sense to use it. Yes it helps, and I should of just printed the function arguments and then printed that it exits the function at the end.

A free service run by Zed A. Shaw for learncodethehardway.org.