dictionary-question

Bas van Gils bas.vangils at home.nl
Wed Nov 21 15:18:55 CET 2001


Hi,

I just found some dictionary-behavior that I don't quite understand:

    Python 2.1.1 (#1, Nov  3 2001, 19:19:22)
    [GCC 2.95.4 20011006 (Debian prerelease)] on linux2
    Type "copyright", "credits" or "license" for more information.
    >>> foo = {}
    >>> type(foo.get("bar",0))
    <type 'int'>
    >>> foo.get("bar",0) += 1
    SyntaxError: can't assign to function call

My current "workaround" is this:

    >>> if foo.has_key("bar"):
    ...     foo["bar"] += 1
    ... else:
    ...     foo["bar"] = 1
    ...
    >>> foo
    {'bar': 1}

(this is just an demonstration of the behavior I don't get ... I want to
use it in a bigger script). 

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?

many thanks in advance

    yours

        Bas

-- 
Bas van Gils <bas.vangils at home.nl>   -    http://members.home.nl/bas.vangils
Build a system that even a fool can use, and only a fool will want to use it.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20011121/9c1b8e0e/attachment.pgp>


More information about the Python-list mailing list