print is not a function

Bengt Richter bokr at oz.net
Thu Oct 9 06:42:55 EDT 2003


On Wed, 08 Oct 2003 11:32:20 +0200, Karl Scalet <news at yebu.de> wrote:

>Hi,
>
>quite often there is a need to just print out the items of a list.
>

>[ prt(x) for x in my_list ]
>
>that would be nice, except prt() does not exist, and print is a
>statement not a function, hence cannot replace prt as of above.
>
>I don't like to write d
>def prt(x):
>     print x
>beforehand and any lambda construct would not be so handy.
>It should be a short one-liner.
>Any ideas?
>

what's wrong with the straightforward suggestion elsewhere in the thread? I.e.,

    for x in my_list: print x

Unless you don't like the many calls to sys.stdout.write that are generated
by that for loop. You can make it into a single write with

    sys.stdout.write('\n'.join(my_list+['']))

But if all your pieces already have newlines, or you just want to concatenate strings
to stdout, note:

 >>> import sys
 >>> help(sys.stdout.writelines)
 """
 Help on built-in function writelines:

 writelines(...)
     writelines(sequence_of_strings) -> None.  Write the strings to the file.

     Note that newlines are not added.  The sequence can be any iterable object
     producing strings. This is equivalent to calling write() for each string.
 """
 >>> sys.stdout.writelines(['<<=starts here,\n','and this is 2nd line\n',
 ... '3rd - no ending newline:', '<<-missing new line\n',
 ... 'last line\n'])
 <<=starts here,
 and this is 2nd line
 3rd - no ending newline:<<-missing new line
 last line

Regards,
Bengt Richter




More information about the Python-list mailing list