try vs. has_key()

Andrew M. Kuchling akuchlin at cnri.reston.va.us
Wed Apr 28 11:11:07 EDT 1999


William H. Duquette writes:
>>>> d = {}
>>>> a = 'Foo'
>>>> d[a] = d.get(a, []).append('Bar')
>>>> d
>{'Foo': None}
>I'd have expected to see {'Foo': 'Bar'}, but that's not what I get.

	The .append() method only returns None, not the list you've
just appended to.  

>>> L = []
>>> print L.append(2)
None
>>> L
[2]

You'd want something like:

dummy = d[a] = d.get(a, [])
dummy.append('Bar')

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
When I originally designed Perl 5's OO, I thought about a lot of this stuff,
and chose the explicit object model of Python as being the least confusing. So
far I haven't seen a good reason to change my mind on that.
    -- Larry Wall, 27 Feb 1997 on perl5-porters





More information about the Python-list mailing list