Which objects are expanded by double-star ** operator?

Terry Reedy tjreedy at udel.edu
Tue Jun 8 15:12:37 EDT 2010


On 6/8/2010 2:18 AM, Terry Reedy wrote:
> On 6/7/2010 6:03 PM, Peter Otten wrote:
>
>> The following experiment shows that you only need to implement a
>> keys() and
>> __getitem__() method.
>>
>> $ cat kw.py
>> class A(object):
>> def keys(self): return list("ab")
>> def __getitem__(self, key):
>> return 42
>>
>> def f(**kw):
>> print(kw)
>>
>> f(**A())
>> $ python kw.py
>> {'a': 42, 'b': 42}
>>
>> However, if you have A inherit from dict...
>>
>> $ cat kwd.py
>> class A(dict):
>> def keys(self): return list("ab")
>> def __getitem__(self, key):
>> return 42
>>
>> def f(**kw):
>> print(kw)
>>
>> f(**A())
>> $ python kwd.py
>> {}
>>
>> it stops working -- probably a side-effect of some optimization.
>> So if you change your hubDict's base class from dict to object you should
>> get the desired behaviour.
>
> In 2.6, the requirement changed from '(subclass of) dictionary' to
> 'mapping' so this is a bit strange. It sort of looks like a bug. I will
> test with 3.1 tomorrow (later today, actually).

Verified and report filed at
http://bugs.python.org/issue8945

Terry Jan Reedy




More information about the Python-list mailing list