Diez B. Roggisch wrote:
> I rarely find things in python strange or named incorrectly, but this is
> IMHO such a case - setdefault led me to think that using it would set a
> default value to return for _future_ lookups of non-existant keys. That
> semantics is known in e.g. ruby or java.
>
> I th
Duncan Booth wrote:
> Diez B. Roggisch wrote:
>
>
>>So if setdefault
>>was implemented as
>>
>>def setdefault(self, v):
>> self["SOME_DEFAULT_KEY_NAME"] = v
>
>
> if setdefault was implemented that way then all current uses of setdefault
> would throw an exception.
>
> setdefault takes *t
> Are we talking about the same setdefault()?
>
>
> D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
>
> There is no per-instance default value just on per call:
Oh. You're right. I was somehow under the impression that setdefault is
per-instance, so that I can avoid
d.get(ke
Diez B. Roggisch wrote:
> So if setdefault
> was implemented as
>
> def setdefault(self, v):
> self["SOME_DEFAULT_KEY_NAME"] = v
if setdefault was implemented that way then all current uses of setdefault
would throw an exception.
setdefault takes *three* parameters: self, key, value. Once
Peter Otten wrote:
> Are we talking about the same setdefault()?
>
> setdefault(...)
>D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
note that it might be spelled "setdefault", but it should be pronounced
"get or set".
--
http://mail.python.org/mailman/listinfo/python
Diez B. Roggisch wrote:
>> The implementation is certainly a design decision. setdefault() could be
>> implemented in terms of __set/getitem__() as
>>
>> def setdefault(self, key, value=None):
>> try:
>> return self[key]
>> except KeyError:
>> self[key] = value
>>
> The implementation is certainly a design decision. setdefault() could be
> implemented in terms of __set/getitem__() as
>
> def setdefault(self, key, value=None):
> try:
> return self[key]
> except KeyError:
> self[key] = value
> return self[key]
>
> I guess it's
Diez B. Roggisch wrote:
> Ron Garret wrote:
>> Is this a bug or a feature?
>>
>> class mydict(dict):
>>def __setitem__(self, key, val):
>> print 'foo'
>> dict.__setitem__(self, key, val)
>>
>>
>d=mydict()
>d[1]=2
>>
>> foo
>>
>d.setdefault(2,3)
>
>
> Feature. If it
Ron Garret wrote:
> Is this a bug or a feature?
>
> class mydict(dict):
>def __setitem__(self, key, val):
> print 'foo'
> dict.__setitem__(self, key, val)
>
>
d=mydict()
d[1]=2
>
> foo
>
d.setdefault(2,3)
Feature. If it wouldn't bypass __setitem__, how exactly would
Is this a bug or a feature?
class mydict(dict):
def __setitem__(self, key, val):
print 'foo'
dict.__setitem__(self, key, val)
>>> d=mydict()
>>> d[1]=2
foo
>>> d.setdefault(2,3)
3
rg
--
http://mail.python.org/mailman/listinfo/python-list
10 matches
Mail list logo