why can't do foo = print ?
steve.menard at polyester.com
Fri Aug 2 22:00:26 CEST 2002
"Steve Holden" <sholden at holdenweb.com> wrote in message
news:aby29.199267$724.121697 at atlpnn01.usenetserver.com...
> "Shagshag13" <shagshag13 at yahoo.fr> wrote in message
> news:aiat1c$12vq6c$1 at ID-146704.news.dfncis.de...
> > 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)
> > how could i do that ? is this possible ? (i can't do "write = print" and
> don't understant why)
> For the same reason you can't do
> define = def
> That is to say, the word "print" is a Python keyword, having a special
> meaning to the parser/compiler, and therefore unusable as an identifier.
you may realize that "print A" is just a shortcust for
so your code could be rewriten like this :
fh = file(filename, 'w')
write = fh.write
write = sys.__stdout__.write
Hope this helps,
More information about the Python-list