Override dict() on class
dict
can be called with an iterable of pairs, so if you design your __iter__
to return an iterable of tuples, your example works as you'd like:
class Foo:
def __iter__(self):
yield from {
'this': 'is',
'a': 'dict'
}.items()
dict(Foo())
{'a': 'dict', 'this': 'is'}
If you want your class to behave like a python dictionary, in that iterating over an instance iterates over its keys, you can implement the interface defined by abc.Mapping
.
You can do this either by implementing __getitem__
, __iter__
, and __len__
, and inheriting from abc.Mapping
, or by implementing all of __getitem__
, __iter__
, __len__
__contains__
, keys
, items
, values
, get
, __eq__
, and __ne__
.
Although the approach in the answer from @ForeverWintr is fairly clever and works, I think it's a little obscure since it takes advantage of some of the arcane details about the attributes of the argument passed to the dict
class constructor.
For that reason a better approach might be what I was saying in my comments about just adding a method that does what you want (plus shows how simply giving it a name indicating exactly what's going on makes it more understandable).
Example code:
class Foo:
def to_dict(self):
return {'this': 'is', 'more': 'clear'}
print( Foo().to_dict() ) # -> {'this': 'is', 'more': 'clear'}