dictionary-question

Laura Creighton lac at strakt.com
Wed Nov 21 13:03:28 EST 2001


> Hello,
> 
> Bas van Gils wrote:
> > Now, can someone please explain why the 
> > 
> >     foo.get("bar",0) += 1
> > 
> > won't work ... and what might be a better sollution then my current
> > workaround?

Aside from the problem of assigning to a function, you appear to want
foo.get("bar",0) to assign foo["bar"] to 0.  It doesn't work that way.
It is just return this argument if you can't do the lookup, not 
return this argument and make the assignment in the dictionary. 

lac at ratthing-b246:~/Mail/boyd/pers$ python
Python 2.1 (#3, May 26 2001, 17:43:53) 
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> foo={'a': 1}
>>> foo.get("bar", 300)
300
>>> print foo
{'a': 1}
>>> 

Laura




More information about the Python-list mailing list