[Python-Dev] Re: dict.setdefault() (Patch#101102) (original) (raw)

Peter Funk pf@artcom-gmbh.de
Tue, 8 Aug 2000 16:48:32 +0200 (MEST)


Hi,

Tim Peters:

>> dict.getorset('hi', []).append(42) # getorset is my favorite

Peter> 'getorset' is a MUCH better name compared to 'default' or Peter> 'setdefault'.

Skip Montanaro:

Shouldn't that be getorsetandget? After all, it doesn't just set or get it gets, but if it's undefined, it sets, then gets.

That would defeat the main purpose of this method: abbreviation. This name is simply too long.

I know I'll be shouted down, but I still vote against a method that both sets and gets dict values. I don't think the abbreviation in the source is worth the obfuscation of the code.

Yes.
But I got the impression that Patch#101102 can't be avoided any more.
So in this situation Tims '.getorset()' is the lesser of two evils compared to '.default()' or '.setdefault()'.

BTW: I think the "informal" mapping interface should get a more explicit documentation. The language reference only mentions the 'len()' builtin method and indexing. But the section about mappings contains the sentence: "The extension modules dbm, gdbm, bsddb provide additional examples of mapping types."

On the other hand section "2.1.6 Mapping Types" of the library reference says: "The following operations are defined on mappings ..." and than lists all methods including 'get()', 'update()', 'copy()' ...

Unfortunately only a small subset of these methods actually works on a dbm mapping:

import dbm d = dbm.open("piff", "c") d.get('foo', []) Traceback (innermost last): File "", line 1, in ? AttributeError: get d.copy() Traceback (innermost last): File "", line 1, in ? AttributeError: copy

That should be documented.

Regards, Peter