print is not a function

Gerhard Häring gh at ghaering.de
Wed Oct 8 06:11:57 EDT 2003


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 ]

This is conceptually dubious. If prt() doesn't return anything, a list 
comprehension is useless. Functions only used as expressions shouldn't 
have side-effects, like writing to stdout.

If you just need an a builtin function that writes to standard output, 
you can use sys.stdout.write().

If you want a one-liner for your above example, something like this 
looks better to me:

print "\n".join([str(x) for x in my_list])

> [...] beforehand and any lambda construct would not be so handy. [...]
> It should be a short one-liner. Any ideas?

It appears that you're suffering from the desease of neat-looking 
one-liners.

I'll try to cure you at the next Stammtisch (local meeting of Pythoneers 
at Munich) :-)

I tend to agree with the Effbot's favourite lambda refactoring rule: 
http://mail.python.org/pipermail/python-list/2001-April/036029.html ;)

-- Gerhard





More information about the Python-list mailing list