python class that acts as mapping for **unpacking -
without subclassing dict, class need considered mapping can passed method **
from abc import abcmeta class uobj: __metaclass__ = abcmeta uobj.register(dict) def f(**k): return k o = uobj() f(**o) # outputs: f() argument after ** must mapping, not uobj
at least point throws errors of missing functionality of mapping, can begin implementing.
i reviewed emulating container types defining magic methods has no effect, , using abcmeta override , register dict validates assertions subclass, fails isinstance(o, dict). ideally, dont want use abcmeta.
the __getitem__()
, keys()
methods suffice:
>>> class d: def keys(self): return ['a', 'b'] def __getitem__(self, key): return key.upper() >>> def f(**kwds): print kwds >>> f(**d()) {'a': 'a', 'b': 'b'}
Comments
Post a Comment