Modifying the {} and [] tokens
Seo Sanghyeon
unendliche at hanmail.net
Sat Aug 23 20:33:19 EDT 2003
Geoff Howland asked:
(about extending builtin literal types.)
You _could_ do that in Python 2.2, though it was a bug, not a feature.
----
Python 2.2
>>> def add(self, key, value):
self[key] = value
>>> dict.add = add
TypeError: can't set attributes of built-in/extension type 'dict'
>>> object.__setattr__(dict, "add", add) # !!
>>> x = {}
>>> x.add(1, 1)
>>> x
{1: 1}
----
Don't tell others that I said this.
Why this is a bug, not a feature? I think that is because Python is not
Ruby. Another evidence is that this behaviour is _fixed_ in Python 2.3:
----
Python 2.3
>>> object.__setattr__(dict, "add", add)
TypeError: can't apply this __setattr__ to type object
----
Obviously somebody put a sanity check there.
Seo Sanghyeon
More information about the Python-list
mailing list