Modifying the {} and [] tokens

Geoff Howland ghowland at
Sat Aug 23 06:32:03 CEST 2003

I want to modify the {} and [] tokens to contain additional
functionality they do not currently contain, such as being able to add
dicts to dicts, and other things that would be very helpful for me and
my team mates to have.

(Please dont kick off a bunch of responses about how this may be a
problem for people supporting code, or copying it out and it doesnt
work.  :) )

I have currently tried this:

>>> class dict(dict):
...     def add(self, name, value):
...         self.__setitem__(name, value)

>>> a = dict()
>>> a.add('five', 'six')
>>> a
{'five': 'six'}

>>> b = {}
>>> dict.add('blah', 5)
Traceback (most recent call last):
  File "<input>", line 1, in ?
AttributeError: 'dict' object has no attribute 'add'

So I can obviously override dict and it works when I specifically call
dict(), but it does not work with the syntax sugar {}.

How can I make this work?  Can this also work to overload strings with
the "" and '' tokens?

-Geoff Howland

More information about the Python-list mailing list