Represent a class as a dict or list

I’ve classes that is used for getting data from one system, making some modifications and then outputting them into another system. Which usually goes the way of converting it into a dict or a list after I’ve made all the necessary conversions.

So far what I’ve done is that I’ve made two methods called as_dict() and as_list() and used that whenever I need that representation.

But I’m curious if there’s a way to be able to do dict(instance_of_my_class) or list(instance_of_my_class).

I’ve been reading up on magic methods and it seems as if this is not possible?

And some simple sample code to work with:

class Cost(object):
    def a_metric(self):
        return self.raw_data.get('a_metric', 0) * 0.8

    # Repeat for various kinds of transformations

    def as_dict(self):
        return {
          'a_metric': self.a_metric,

Best answer

Do you mean something like this? If so you have to define a __iter__ method that yield’s key-value pairs:

In [1]: class A(object):
   ...:     def __init__(self):
   ...:        self.pairs = ((1,2),(2,3))
   ...:     def __iter__(self):
   ...:         return iter(self.pairs)

In [2]: a = A()

In [3]: dict(a)
Out[3]: {1: 2, 2: 3}

Also, it seems that dict tries to call the .keys / __getitem__ methods before __iter__, so you can make list(instance) and dict(instance) return something completely different.

In [4]: class B(object):
    ...:     def __init__(self):
    ...:        self.d = {'key':'value'}
    ...:        self.l = [1,2,3,4]
    ...:     def keys(self):
    ...:         return self.d.keys()
    ...:     def __getitem__(self, item):
    ...:         return self.d[item]
    ...:     def __iter__(self):        
    ...:         return iter(self.l)

In [5]: b = B()

In [6]: list(b)
Out[6]: [1, 2, 3, 4]

In [7]: dict(b)
Out[7]: {'key': 'value'}