why can't do foo = print ?

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Thu Aug 1 14:55:15 CEST 2002


----- Original Message ----- 
From: "Heiko Wundram" <heikowu at ceosg.de>


> What you might do is the following:
> 
> if output_is_file:
>   fh = open(filename,'w')
>   my_output = fh.write
> else:
>   my_output = sys.stdout.write
> 
> for i in something:
>   my_output(str(i))

This, to me, is strange (though I can see using it in some 
cases).  I would rather see something like this:

    if output_is_file:
        fh = open(filename, "w")
        sys.stdout = fh

then just use print as usual everywhere.  This assumes that
you really want to only send "print" output to one place
all the time (for instance, if you plan to do this at the 
top of the main module).

You might also say this:

    if output_is_file:
        fh = open(filename, "w")
        old_stdout = sys.stdout
        sys.stdout = fh

so you can put it back if needed.


Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net





More information about the Python-list mailing list