Python Oddity - print a reserved name

Duncan Booth duncan.booth at invalid.invalid
Wed Sep 15 14:36:31 CEST 2004

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.

More information about the Python-list mailing list