Modifying the {} and [] tokens

Geoff Howland ghowland at lupineNO.SPAMgames.com
Sat Aug 23 19:10:41 CEST 2003


On Sat, 23 Aug 2003 12:40:05 -0400, Roy Smith <roy at panix.com> wrote:

>Geoff Howland <ghowland at lupineNO.SPAMgames.com> wrote:
>> {} + {} is not allowed.
>
>What would it mean?  What would you do if you had two dicts:
>
>d1 = {'a':1, 'b':2}
>d2 = {'a':42}
>
>what would the value of (d1+d2)['a'] be?

I dont think it matters.  Pick one and make that the standard.

The current norm is "last one in clobbers the existing", so keep that
and d = d1 + d2 ; d['a'] == 42

I really dont think it matters that its not perfect, what matters is
that it's possible and if you have unique keys, or dont care about
clobbering, then you dont have to do this yourself.

If you do care, you have to write your own routine.  As it stands now,
everyone has to do this every time, even if they would never encounter
the clobbering situation, or dont care.

It's the same reason an exception isnt thrown for this:

{'a':5, 'a':6}

Obviously, you cant have both, but it's legal.  As long as the rules
are defined and it stays consistent every time, then it can be of use.

Same for subtraction to remove overlaps, etc.  It's an operator, so it
only has one use per term.  Even if it's not handling all cases, why
make everyone re-implement the same one 80% of the time?

In my work, this would be useful functionality, and maybe other places
it would be dangerous and thus it shouldnt be added to the language
because it's better not to mess with it.  That's fine, then I was
still looking for a way to make it work just with _my_ code.  Since it
is now clear to me that this isn't possible with the {} tokens, then I
will just go about it the way that does work.  :)

I just wanted the best solution for my own problem at hand.


-Geoff Howland
http://ludumdare.com/




More information about the Python-list mailing list