Modifying the {} and [] tokens

Andrew Dalke adalke at mindspring.com
Sat Aug 23 06:41:20 CEST 2003


Geoff Howland:
> 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.

You can't.  Not without changing the underlying implementation.

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

That's because you created a new dict.  You didn't modify the
actual type {} uses in the intepreter.  {} is not simple syntactic sugar
for dict() in the local namespace.  BTW, if it were, then

  dict = 8
  a = {3: 4}

would fail.

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

You can't.  What's wrong with using your derived dict?  Why must you
use {}?

You can't modify the fundamental type used by strings either.

                    Andrew
                    dalke at dalkscientific.com






More information about the Python-list mailing list