why can't do foo = print ?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Aug 1 12:12:23 CEST 2002


On Thursday 01 Aug 2002 10:09 am, Roman Yakovenko wrote:
> Sure,
>
> import sys
> sys.stdout.write("wer")
>
> Roman
>
> -----Original Message-----
> From: Shagshag13 [mailto:shagshag13 at yahoo.fr]
> Sent: Thursday, August 01, 2002 10:56 AM
> To: python-list at python.org
> Subject: why can't do foo = print ?
>
> hello,
>
> i would like to have something like :
>
> if output_is_file:
>     fh = file(filename, 'w')
>     write = fh.write
> else:
>     write = print
>
> ...
>
> for i in something
>     write(i)

so your code becomes....


if output_is_file:
    fh = file(filename, 'w')
    write = fh.write
else:
    write = sys.stdout


as sys.stdout is a file 'like' object.






More information about the Python-list mailing list