why can't do foo = print ?

Heiko Wundram heikowu at ceosg.de
Thu Aug 1 11:18:25 CEST 2002

Hi lt!

What you can't do is assign the print to anything, as "print-Statements"
are a grammatical construct (a _special_ grammatical construct)
different from having a function called write in a filehandle, which you
can happily assign to a variable. print is no function in the normal

What you might do is the following:

if output_is_file:
  fh = open(filename,'w')
  my_output = fh.write
  my_output = sys.stdout.write

for i in something:

btw: Remind yourself that there is a big difference between doing a
write and a formatted output using print (more than just the newline
print automatically appends when there's no comma at the end of the
string). If you want equal behaviour, use the above (sys.stdout is a
reference to file handle 0, which also has a write attribute).

And also mind the str() done on the stuff you want to output; if I
remember correctly the write functions will choke when you don't pass
them a string.


	Heiko W.

More information about the Python-list mailing list