dict.setdefault() (Patch#101102) (was: Re: [Python-Dev] Re: A small proposed change to dictionaries' "get" method...)

artcom0!pf@artcom-gmbh.de artcom0!pf@artcom-gmbh.de
Mon, 7 Aug 2000 10:14:54 +0200 (MEST)


Hi,

Guido:
>     >> dict.default('hello', []).append('hello')

Greg Ewing <greg@cosc.canterbury.ac.nz>:
>     GE> Is this new method going to apply to dictionaries only,
>     GE> or is it to be considered part of the standard mapping
>     GE> interface?
 
Barry A. Warsaw:
> I think we've settled on setdefault(), which is more descriptive, even
> if it's a little longer.  I have posted SF patch #101102 which adds
> setdefault() to both the dictionary object and UserDict (along with
> the requisite test suite and doco changes).

This didn't answer the question raised by Greg Ewing.  AFAI have seen,
the patch doesn't touch 'dbm', 'shelve' and so on.  So from the patch
the answer is "applies to dictionaries only".

What is with the other external mapping types already in the core,
like 'dbm', 'shelve' and so on?

If the patch doesn't add this new method to these other mapping types, 
this fact should at least be documented similar to the methods 'items()' 
and 'values' that are already unimplemented in 'dbm':
 """Dbm objects behave like mappings (dictionaries), except that 
    keys and values are always strings.  Printing a dbm object 
    doesn't print the keys and values, and the items() and values() 
    methods are not supported."""

I'm still -1 on the name:  Nobody would expect, that a method 
called 'setdefault()' will actually return something useful.  May be 
it would be better to invent an absolutely obfuscuated new name, so 
that everybody is forced to actually *READ* the documentation of this 
method or nobody will guess, what it is supposed to do or even
worse: how to make clever use of it.

At least it would be a lot more likely, that someone becomes curious, 
what a method called 'grezelbatz()' is suppoed to do, than that someone
will actually lookup the documentation of a method called 'setdefault()'.

If the average Python programmer would ever start to use this method 
at all, then I believe it is very likely that we will see him/her
coding:
	dict.setdefault('key', [])
	dict['key'].append('bar')

So I'm also still -1 on the concept.  I'm +0 on Gregs proposal, that
it would be better to make this a builtin function, that can be applied
to all mapping types.

Maybe it would be even better to delay this until in Python 3000
builtin types may have become real classes, so that this method may
be inherited by all mapping types from an abstract mapping base class.

Regards, Peter
-- 
Peter Funk, Oldenburger Str.86, D-27777 Ganderkesee, Germany, Fax:+49 4222950260
office: +49 421 20419-0 (ArtCom GmbH, Grazer Str.8, D-28359 Bremen)