Python Oddity - print a reserved name
Diez B. Roggisch
deetsNOSPAM at web.de
Wed Sep 15 21:39:05 EDT 2004
> Why? If a print statement were just syntactic sugar for print((outfile,),
> ... rest, of, line) (normally calling a builtin) then, giving your example
> args,
>
> def print(*args):
> import sys
> sys.stdout.write( repr(args)+'\n')
>
> print print, 1, 'two', 3
>
> would output (faked ;-)
>
> '((), <function print at 0x008FDE70> 1, "two", 3)'
How should that happen? As
print
produces a newline, one should expect that
print print
gets translated to
print(outfile, print(outfile,))
which would clearly produce two newlines and raises the question what the
inner print gets evaluated to so that the outer one wouldn't print
anything.
While that would be sort of accetable, what you suggest makes print
ambigious - depending on the context. And above that:
>>> len
<built-in function len>
>>>
has no sideeffects whatsoever, where
>>> print
>>>
clearly has.
As BDFL has already said, he regrets to have introduced print as a statement
- but as he did, there is no really elegant way around having print as a
reserved keyword. One _can_ think of taking context into account to either
interpret print as keyword or as identifier while parsing - the question
is: Is it worth it? IMHO no - but as Alex said: If it makes you happy,
implement it :) The same applies to all other keywords as well, btw.
--
Regards,
Diez B. Roggisch
More information about the Python-list
mailing list