3

I am using pdb to debug a program. I successively hit 'c' to run through the code and at each step pdb shows me which line is executed.

Let's say we have this code:

def foo(bar):
   print(bar)

foo('hey')

First, line 4 calls function foo. Then pdb shows me the line

def foo(bar)

is executed.

Why? Is not that line just a kind of label? What happens before "print(bar)" is executed? (that comes with another 's' hit)

EDIT: I experimented that something done is to actually check the definition. In fact, in the case foo were a generator (that cannot be called in such a way) python still gets there and then decides to treat it as a generator (or a function depending the case..).

Niccolò
  • 2,854
  • 4
  • 24
  • 38
  • Good question! I'm looking forward to reading an answer. I would say, though, that it would be even more surprising if it did skip. How else would it validate parameters, etc? (If it skipped, how would it know to complain if you called `foo(1, 2)`?) – vroomfondel Sep 13 '13 at 01:20
  • Yep, but I need to know exactly what it happens there. You @rogaos might be right.. However, in the case you suggested pdb stops its execution at foo(1,2) and never gets to def line. So when we see the def line it has already accepted/matched the function. Note that next 's' hit gets to the print line, and as always it stops **before** executing the line. So you can inspect the stack/heap before anything happens in the function. – Niccolò Sep 13 '13 at 01:40

2 Answers2

8

def is not a declaration in Python, it's an executable statement. At runtime it retrieves the code object compiled for the function, wraps that in a dynamically created function object, and binds the result to the name following the def. For example, consider this useless code:

import dis
def f():
    def g():
        return 1
dis.dis(f)

Here's part of the output (Python 2.7.5 here):

0 LOAD_CONST               1 (<code object g at 02852338, file ...>)
3 MAKE_FUNCTION            0
6 STORE_FAST               0 (g)

All this is usually an invisible detail, but you can play some obscure tricks with it ;-) For example, think about what this code does:

fs = []
for i in range(3):
    def f(arg=i**3):
        return arg
    fs.append(f)
print [f() for f in fs]

Here's the output:

[0, 1, 8]

That's because the executable def creates three distinct function objects, one for each time through the loop. Great fun :-)

Tim Peters
  • 67,464
  • 13
  • 126
  • 132
1

What happens before "print(bar)" is executed?

This is just an educated guess: I suppose the current IP is pushed onto the stack and then the parameters. Then a new stack frame is created, the parameters are popped from stack and added as locals to the current scope. Something along this line.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • Sounds reasonable. Now, I'm facing a particular situation in which something goes wrong at the def line of a function. The program gets there and.. does not execute the function! Only with pdb I notice the message "GeneratorExit: None". So, any guess of what can go wrong at that step? Note I'm trying to make a minimal example to reproduce that, in the meanwhile it seemed an interesting question per se and I posted it. – Niccolò Sep 13 '13 at 01:59
  • So, turns out mine was a generator not a classic function. Thus, one thing that it does there is to check what it is. http://stackoverflow.com/questions/18777544/dynamic-call-of-functions-and-generator-function-python/18777629?noredirect=1#18777629 – Niccolò Sep 13 '13 at 03:21