dictionary-question

Michael Hudson mwh at python.net
Wed Nov 21 13:44:27 EST 2001


Bas van Gils <bas.vangils at home.nl> writes:

> Now, can someone please explain why the 
> 
>     foo.get("bar",0) += 1
> 
> won't work 

Because Python doesn't have a generalised notion of place.  The only
things you can assign to are 

 * names
 * attributes
 * subscripts (including slices)

This is fixed in the syntax.  Common Lisp does have a more general
notion of place, so you *can* do

    (incf (gethash foo "bar" 0))

(which is basically equivalent to what you want to do above), but it's
a fairly complicated facility.

> ... and what might be a better sollution then my current
> workaround?

foo["bar"] = foo.get("bar", 0) + 1

is what foo.get("bar", 0) + 1 would end up doing anyway.  Well,
without significant additional complexity, anyway.

Cheers,
M.

-- 
  Clue: You've got the appropriate amount of hostility for the
  Monastery, however you are metaphorically getting out of the
  safari jeep and kicking the lions.                         -- coonec
               -- http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html



More information about the Python-list mailing list