Does line 5 ever get called? I’m new and trying to learn program tracing.
Thanks in advance.
Hello! Welcome to the forum!
A “line”, per se, is never called. A function can be called, even a class (a callable
, generically speaking).
What do you mean with “line 5 get called”? The Python interpreter goes through the whole file, and in line 5 finds an object being called, that object is add5
(defined in line 1), and passes as an argument a 3
.
FYI Please post code as pre-formatted text not screen shots you can use the </>
button to do this. If it was text I could quote it and give you an updated copy.
To understand what you code does you can add print() calls to see where the code goes.
On line 5 try adding print('At line 5')
to the code to see what happens.
To answer your question, yes, line 5 (add5(3)
) does run. You can see the exact order yourself by learning to use a debugger (which I highly recommend even as a beginner) or copy/pasting your code into pythontutor.com.
Thank you.
Run this in the debugger and step through it using the “n” command. There are several debuggers you can use but pdb
comes with Python. So run your program like this: python -m pdb myprog.py
. You will then see a debugger prompt.
A debugger can also use aliases as defined in a .pdbrc
file in the same folder as your python program.