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

Ian Kelly ian.g.kelly at gmail.com
Mon Jun 7 18:32:53 EDT 2010


On Mon, Jun 7, 2010 at 4:03 PM, Peter Otten <__peter__ at web.de> 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}

This seems to require Python 2.6.  With 2.5, you get:

Traceback (most recent call last):
  File "kw.py", line 9, in <module>
    f(**A())
TypeError: f() argument after ** must be a dictionary

Cheers,
Ian



More information about the Python-list mailing list