What eval
does it to evaluate a string as a Python expression.
You do not want to call eval
on your objects to get their representation. pdb
/WinPDB
does not call eval
on your objects to get their representation. In fact, eval
is nearly the opposite of repr
.
If you want to see the results of foo.__repr__()
, you call repr(foo)
—or just type foo
at the console. Typing any expression at the console causes it to show you repr(that expression)
.
Of course this means that if you type eval(foo)
at the console, what you will see is repr(eval(foo))
. But that has nothing to do with eval
; it's the same as any other expression.
Meanwhile, at the Winpdb console, you're not running Python code, you're running Winpdb code. Winpdb has a command named eval
, which is similar to the Python function eval
, but it's not the same thing.
As the documentation says, eval foo
effectively evaluates foo
in your live debuggee session. Notice that it a fragment that's parseable as a Python expression, not a string holding such a fragment.
So, eval(variable)
in the Winpdb console is similar to eval("(variable)")
in the interactive interpreter. Which is nearly identical to just (variable)
in the interactive interpreter. Which is equivalent to variable
.
But if you type variable
(or (variable)
or eval("(variable)")
) in your interactive interpreter, it prints out repr(variable)
, which calls your __repr__
method. Why isn't that happening in Winpdb?
Because printing out the repr
of any expression you type at the interactive interpreter is a feature of the interactive interpreter, not of the language. While the Winpdb console could do the same thing, it doesn't. (This makes it possible to debug problems with __repr__
methods, and it avoids running code from the debuggee in the debugger without making it explicit.)
So, what about eval(variable.__repr__())
. As you can probably guess, that's like evaluating the string "(variable.__repr__())"
in the debuggee. That .__repr__()
ends up being part of the expression you evaluate in the debuggee environment, so of course it gets called in the debuggee environment. The result is not a UserDict
object, as in the previous version, but a string.
So, finally, what exactly does Winpdb show in its Repr column and as the result of calling eval
? Well, that isn't really documented explicitly, so you can either figure it out through trial and error, or read the source.