Python Oddity - print a reserved name

Bengt Richter bokr at oz.net
Wed Sep 15 18:58:51 EDT 2004


On Wed, 15 Sep 2004 23:09:22 +0200, "Diez B. Roggisch" <deetsNOSPAM at web.de> wrote:

>Michael Foord wrote:
>> Right - but although 'print' is a reserved word there is no *need* for
>> object.print to be reserved.. and as Alex has pointed out that could
>> actually be damned inconvenient..........
>
>I tried to explain my views on that before:
>
>http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&threadm=cacto9%24ql8%2403%241%40news.t-online.com&rnum=4&prev=/groups%3Fq%3Ddiez%2Broggisch%2Bfoo%26hl%3Den%26lr%3D%26ie%3DUTF-8%26c2coff%3D1%26selm%3Dcacto9%2524ql8%252403%25241%2540news.t-online.com%26rnum%3D4
>
>The key issue is, that while
>
>>>> def foo():
>>>>    pass
>>>> print foo
><function foo at 0x401eab1c>
>
>is ok, 
>
>>>> def print(): pass
>fails here, but if not
>>>> print print
>
>can't possibly made working without unclear context-driven hacks.
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)'

instead of blowing up, and the _statement_

    print(print)

would, de-sugared, act like print((), (print)) and thus output

'((), <function print at 0x008FDE70>)'

without the def print(...

    print print

would de-sugar to

    print((), print)

and output

<built-in function print>

and

    type(print)

would not be a print statement, so would return

<type 'builtin_function_or_method'>

interactively.
Etc., etc.
>
>And if on "normal" function level this can't be allowed, IMHO  for the sake
>of consistency class methods should also not allow that - because then the
>different behaviour causes confusion...

Maybe there's a problem with my suggestion, but I don't see it at the moment.

Regards,
Bengt Richter



More information about the Python-list mailing list