Python Oddity - print a reserved name
Michael Foord
fuzzyman at gmail.com
Wed Sep 15 15:57:35 EDT 2004
Duncan Booth <duncan.booth at invalid.invalid> wrote in message news:<Xns95658A6A9564Eduncanrcpcouk at 127.0.0.1>...
> Michael Foord wrote:
> > >>> something.__dict__['print'] = 3
>
> Or, slightly prettier, use:
>
> setattr(something, 'print', 3)
>
> > See that I can't set the something.print attribute directly, but can
> > set it indirectly. Is this behaviour 'necessary' or just an anomaly of
> > the way IDLE detects Syntax Errors ?
> >
>
> No, that is simply how Python works. You can only use the direct syntax to
> set attributes whose names are valid Python identifiers, but indirectly you
> can use any string at all as the attribute name. It doesn't do any harm and
> sometimes it can be extremely useful.
>
> You can do this pretty much anywhere that Pythonn uses a dict internally.
> For example you can call functions with arbitrary keyword arguments
> provided you use the ** syntax.
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..........
Oh well.....
Regards,
Fuzzy
http://www.voidspace.org.uk/atlantibots/pythonutils.html
More information about the Python-list
mailing list