print is not a function

Bengt Richter bokr at oz.net
Thu Oct 9 16:51:28 EDT 2003


On Wed, 08 Oct 2003 17:15:30 +0200, Karl Scalet <news at yebu.de> wrote:

>Karl Scalet wrote:
>
>> Hi,
>> 
>> quite often there is a need to just print out the items of a list.
>> 
>> [ prt(x) for x in my_list ]
>> 
>....
>
>Thank you all for the overwhelming sympathy to my original problem :-)
>
>To clarify a bit:
>
>I'm no fan of unreadable oneliners, normally, or making things more
>complicated as they are.
>I was just looking for the easiest way to *inspect*
>*interactively* lists, dictionaries.
>Evaluating such a list via
> >>> print my_list
>or even
> >>> my_list
>does not give very readable results, especially if those
>lists tend to become longer.
>
>So my own (best??) solution is:
>
> >>> from pprint import pprint as prt # cannot overcome this line :-(
> >>> from qt import * # now the secret unveils
> >>> [ prt(x) for x in locals() if x[:2]=='QA' ]
># sorry Signore Alex, but it's shorter than startswith :-)
># gives me
> >>> 'QAssistantClient'
> >>> 'QAccel'
> >>> 'QActionGroup
> >>> 'QApplication'
> >>> 'QAction'
> >>> [None, None, None, None, None] # well, I can live with
You don't have to, if you write (untested)

    [0 for x in locals() if x[:2]=='QA' and prt(x)] or None

or if you make a prt that can return other than None, you can guarantee the
if failure by writing

    [0 for x in locals() if x[:2]=='QA' and prt(x) and 0] or None
>
>In the case of doing such interactive inspections, i don't
>mind about potential side-effects, i actually don't see them here.
>
Interactively, UIAM for some case, locals()==globals(), so if you want to
avoid typing that more than once, you might want to make a function, e.g. (untested)

    pqa = lambda:[0 for x in globals() if x[:2]=='QA' and prt(x)] or None

and then just type pqa()

But a better idea is to code yourself some simple utility functions in the most
straightforward way, with simple doc strings, and put them in a module that you can
import from, and check on what you did, e.g., I don't always take my own advice, but

 >>> from ut.miscutil import pl
 >>> help(pl)
 Help on function pl in module ut.miscutil:

 pl(alist, cols=None, width=78)
     prints a list one left-justified item per line
     or in cols columns across width(default 78)

For almost-throwaways relating only to a particular project, just accumulate them
in e.g., goodies.py in the same directory, and you can interactively type

    from goodies import *

and be ready to go.

Regards,
Bengt Richter




More information about the Python-list mailing list