Best way to print a module?
steve+comp.lang.python at pearwood.info
Tue Sep 6 01:46:16 CEST 2011
Tim Roberts wrote:
> Martin De Kauwe <mdekauwe at gmail.com> wrote:
>>If I wanted to print an entire module, skipping the attributes
>>starting with "__" is there an *optimal* way?
> Your question is somewhat ambiguous. When I read "print an entire
> module", I assumed you were asking for a way to print the source code,
> perhaps with syntax coloring.
> Surely there is no reason to have an "optimal" method of doing this --
> this is never going to be in an inner loop.
Regardless of an inner loop or not, the time required for IO (reading the
file from disk, writing it to a printer) will be much larger than the time
required to skip dunder (double-underscore) objects.
> If you have a method that works,
> there is little justification to optimize...
HOWEVER, having agreed with you in general, in this specific case it is
obvious to me from context that the OP doesn't want to print the source
code of the module, but the module's names and their values.
I'd try a couple of approaches:
- use dir() or vars() to get the module's names, then loop and print each
- make a shallow copy of the module __dict__, less dunder names, and pass it
to the prettyprint module for printing.
More information about the Python-list