45 lines
1.2 KiB
Plaintext
45 lines
1.2 KiB
Plaintext
From: akuchlin at cnri.reston.va.us (Andrew M. Kuchling)
|
|
Date: Wed, 28 Apr 1999 11:11:07 -0400 (EDT)
|
|
Subject: try vs. has_key()
|
|
In-Reply-To: <37247ea3.494305@news.jpl.nasa.gov>
|
|
References: <aahzFAM4oJ.M7M@netcom.com>
|
|
<yWOT2.6007$8m5.9320@newsr1.twcny.rr.com>
|
|
<Pine.SUN.3.95-heb-2.07.990423140345.21577A-100000@sunset.ma.huji.ac.il>
|
|
<37247ea3.494305@news.jpl.nasa.gov>
|
|
Message-ID: <14119.9202.870049.51888@amarok.cnri.reston.va.us>
|
|
X-UID: 94
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|