A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/2004-January/042129.html below:

[Python-Dev] dict.addlist()

[Python-Dev] dict.addlist()Nick Coghlan ncoghlan at iinet.net.au
Tue Jan 20 10:14:12 EST 2004
Barry Warsaw wrote:

> On Tue, 2004-01-20 at 09:25, Kevin Jacobs wrote:
> 
> 
>>-1: I use .setdefault all the time with non-list default arguments.  
>>
>>The dict interface is getting complex enough that adding more clutter will
>>make it even harder to teach optimal idiomatic Python to newbies.
> 
> I completely agree with both points.

As a relative newbie (and not having done much coding in recent months), 
I have to say it took me a while to figure out what "d.setdefault(k, 
[]).append(v)" actually did (I had previously only encountered 
dict.getdefault, and the meaning of dict.setdefault was not immediately 
obvious to me).

If I saw "d.addlist(some_variable, some_other_variable)", I certainly 
would not automatically interpret it as "append some_other_variable to 
the value keyed by some_variable, creating that value as the empty list 
if it is not present". At least the current approach breaks this into 
two steps, and gave me a chance to figure it out without diving into the 
docs to find out what the method does.

This does seem to be another example where a 'defaulting dictionary' 
with a settable factory method would seem to be useful.

Then:

dd = defaultingdict(factory=list)
...other code uses dict...
dd[k].append(v)


(As others have pointed out, this can't be a keyword argument on 
standard dictionaries without interfering with the current automatic 
population of the created dictionary with the keyword dictionary)

Regards,
Nick.

-- 
Nick Coghlan               |     Brisbane, Australia
Email: ncoghlan at email.com  | Mobile: +61 409 573 268


More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4