<dict>.setdefault()

Steven Taschuk staschuk at telusplanet.net
Thu Jul 31 21:06:32 EDT 2003


Quoth Tino Lange:
> On 31 Jul 2003 18:01:51 +0100, jjl at pobox.com (John J. Lee) wrote:
  [...]
> >Don't, then.  :-)  Use key in dict, or dict.has_key(key).
> 
> or the C-like style:
> >>> a[3] = a.get(1) or b.setdefault(4,1)

Be careful doing that -- it relies on all possible values for a[1]
being true.  Consider:

    >>> a = {1: 0}
    >>> b = {} 
    >>> a[3] = a.get(1) or b.setdefault(4, 1)
    >>> a
    {1: 0, 3: 1}
    >>> b
    {4: 1}

-- 
Steven Taschuk                                                 o- @
staschuk at telusplanet.net                                      7O   )
                                                               "  (





More information about the Python-list mailing list