[issue8945] Bug in **kwds expansion on call?

Terry J. Reedy report at bugs.python.org
Tue Jun 8 21:11:30 CEST 2010


New submission from Terry J. Reedy <tjreedy at udel.edu>:

In 2.6, the requirement for **kwds keyword argument expansion in calls (LangRef 5.3.4. Calls) is relaxed from "(subclass of) dictionary" (2.5) to "mapping".  The requirement in this context for 'mapping' is not specified. LRef3.2 merely says "The subscript notation a[k]  selects the item indexed by k from the mapping a;". Here, .keys seems to be needed in addition to .__getitem__. (.items alone does not make an object a mapping.)

In python-list thread "Which objects are expanded by double-star ** operator?", Peter Otten posted 2.6 results for

class A(object):
     def keys(self): return list("ab")
     def __getitem__(self, key):
         return 42
        
class B(dict):
     def keys(self): return list("ab")
     def __getitem__(self, key):
         return 42

def f(**kw):
     print(kw)

f(**A())
# {'a': 42, 'b': 42}

b = B(); print(b['a'], b['b']) # I added this
# 42, 42
f(**b)
# {}

I get same with 3.1. It appears .keys() is called in the first case, but not the second, possibly due to an internal optimization.

The different of outcome seems like a bug, though one could argue that the doc is so vague that it makes no promise to be broken.

----------
components: Interpreter Core
messages: 107338
nosy: tjreedy
priority: normal
severity: normal
status: open
title: Bug in **kwds expansion on call?
type: behavior
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8945>
_______________________________________


More information about the Python-bugs-list mailing list