print is not a function

Wojtek Walczak gminick at hacker.pl
Wed Oct 8 09:56:51 EDT 2003


Dnia Wed, 08 Oct 2003 12:22:12 +0200, Karl Scalet napisał(a):
>>>>>import sys
>>>>>sys.stdout.write("Hello world!\n")
>
> I came across this, but this requires an extra import sys
> which not always is there, thanks anyhow.

It's ugly, but works:
[__import__('sys').stdout.write(str(i)+'\n') for i in range(5)]

or even:
[__import__('os').write(1, str(i)+'\n') for i in range(5)]

The main difference is that first will return an array of Nones
while the second will return an array of values returned by os.write().
Personally I prefer using print statement and join() method:

print '\n'.join([str(i) for i in range(5)])

-- 
[ Wojtek Walczak - gminick (at) underground.org.pl ]
[        <http://gminick.linuxsecurity.pl/>        ]
[ "...rozmaite zwroty, matowe od patyny dawnosci." ]





More information about the Python-list mailing list