Modifying the {} and [] tokens

Geoff Howland ghowland at lupineNO.SPAMgames.com
Sat Aug 23 01:48:22 EDT 2003


On Sat, 23 Aug 2003 04:41:20 GMT, "Andrew Dalke"
<adalke at mindspring.com> wrote:

>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.

The implementation in the C code, or implementation at the Python
level?

As long as this stays in Python, like as a module that can be included
or not, then I think it would be helpful.  I think a lot of the things
I would like to do might actually be good in the language, but maybe
they've already been thrown out, or something.  For now, I dont have
an entire list and implementation of everything I would like to see to
really present as a "possible change list", so I'd just like to be
able to do this to assist in my own teams work.

There is history and other info beyond this besides "I think it would
be neat", but that is team private business so I dont want to drag it
out here.  This would just be helpful.

>> 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.

Right, so is there a way to change this?  I know the syntax I tried
doesnt work and for obvious type reasons, since you wouldnt want to do
this by accident.

Perhaps something has to be registered in some Python
variable/class/module-list I am not aware of that would make this
happen?  I am ok with the consequences of potentially introducing
oddities into my code from the Norm for this flexibility.

>> 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 {}?

The reason for using it is that it is easier to code using the default
containers than having to specify things.  IMO, there are some things
missing from the language, and while maybe they wont be added, I would
like to have them.

{} + {}, {} - {}.  [] - [], etc

These operations would be useful, as well as additional functions that
are commonly performed and have to be put in separate modules.

I would also like to move all the usual operational functions into
some of these, such as instead of len([]), you could do [].len().
More consistent for some of my team to read.

If it's impossible without creating a new EXE, then I wont have a
choice.  If it is not impossible, then I would like to be able to add
some things to the standard containers that are already very powerful
but seem to be missing some basics to complete their operational sets.

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

I'd have the same questions above for this.  :)


-Geoff Howland
http://ludumdare.com/




More information about the Python-list mailing list