Modifying the {} and [] tokens

Michele Simionato mis6 at pitt.edu
Sat Aug 23 17:18:49 CEST 2003


Geoff Howland <ghowland at lupineNO.SPAMgames.com> wrote in message news:<04cekv0s6ghjtuhl5j2gph7ltobdb5rsam at 4ax.com>...
> On 23 Aug 2003 07:02:53 GMT, "OKB (not okblacke)" <BrenBarn at aol.com>
> wrote:
> 
> >> {} + {}, {} - {}.  [] - [], etc
> >
> >    	I'm a little puzzled as to why you'd need these, since it seems 
> >like all they allow you to is hard-code a certain combination of 
> >statically defined objects.  That is, if you're going to do
> >
> >{ 'a': 1, 'b': 2 } + { 'c': 3, 'd': 4}
> >
> >    . . .why not just write
> >
> >{ 'a': 1, 'b': 2, 'c': 3, 'd': 4}
> >
> >    	in the first place?
> 
> Because I wouldn't just be using {} on a line, I would create a
> dictionary with it and then use it around, and then at some point
> later I may want to add another dictionary to it, or find the union or
> differences between two dictionaries.
> 
> Ruby has this built in, it's useful, it's complete.  It's obvious what
> is happening, so it's not unclean.  It's allowed for other containers.
> 
> 
> -Geoff Howland
> http://ludumdare.com/

Subclass dict, define __add__ and __sub__ and it will work, but NOT
with the brace syntax. You must use something like

mydict(a=1,b=2)+mydict(c=3,d=4)

Python tries hard not to modify its basic syntax, so you must stay
with the above. Pythonista would argue that this is a strenght of the 
language: "explicit is better than implicit".

Michele Simionato, Ph. D.
MicheleSimionato at libero.it
http://www.phyast.pitt.edu/~micheles
--- Currently looking for a job ---




More information about the Python-list mailing list