dir() vs print(dir()) in the embedded mode

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Mar 29 18:16:20 EDT 2013


On Fri, 29 Mar 2013 16:04:24 -0500, Nick Gnedin wrote:

> Folks,
> 
> I have a newbie question: I am trying to embed Python into my
> application. While playing around, I noticed that the behavior of the
> interpreter in the embedded mode differs from the standalone one.
> 
> Namely, in the standalone mode if I type dir(), I get a list of build-in
> symbols. In the embedded mode only print(dir()) does that, while just
> dir() returns silently.

What do you mean by "standalone mode"? If you are talking about the 
interactive interpreter, that is expected behaviour. The interactive 
interpreter automatically prints the output of every line.

When running non-interactively as a script (whether embedded or not), if 
you want to print the output of a line, you will have to call print on 
that output.


> Is there a way to intercept the output of dir() (and all other commands)
> and display them to the user?

Automatically? I don't believe so. I think it is hard-coded behaviour of 
the interactive interpreter, and cannot by enabled for scripts.



-- 
Steven



More information about the Python-list mailing list