I have a class where I want to get the object back as a dictionary, so I implemented this in the __dict__()
. Is this correct?
I figured once I did that, I could then use the dict
(custom object), and get back the object as a dictionary, but that does not work.
Should you overload __dict__()
? How can you make it so a custom object can be converted to a dictionary using dict()
?
No.
__dict__
is a method used for introspection - it returns object attributes. What you want is a brand new method, call itas_dict
, for example - that's the convention. The thing to understand here is thatdict
objects don't need to be necessarily created withdict
constructor.__dict__
is not a special method on Python objects. It is used for the attribute dictionary;dict()
never uses it.Instead, you could support iteration; when
dict()
is passed an iterable that produces key-value pairs, a new dictionary object with those key-value pairs is produced.You can provide an iterable by implementing a
__iter__
method, which should return an iterator. Implementing that method as a generator function suffices:Demo:
You could also subclass
dict
, or implement the Mapping abstract class, anddict()
would recognize either and copy keys and values over to a new dictionary object. This is a little more work, but may be worth it if you want your custom class to act like a mapping everywhere else too.