Custom class to a dictionary?

O

Oliver Beattie

Just wondering if it is possible to pass a custom class instance
instance to dict() by way of using methods like you can for iterators
(__iter__, __getitem__ etc.) I see there is no __dict__ -- is there
anything else I can use to achieve this?

Kind Regards,
Oliver
 
S

Steven D'Aprano

Just wondering if it is possible to pass a custom class instance
instance to dict() by way of using methods like you can for iterators
(__iter__, __getitem__ etc.) I see there is no __dict__ -- is there
anything else I can use to achieve this?


Just write a method to return (key, value) pairs, and call that:
.... def __init__(self):
.... self.keys = [1, 2, 3, 4]
.... self.values = ["one", "two", "three", "four"]
.... def generate_tuples(self):
.... for k,v in zip(self.keys, self.values):
.... yield (k,v)
....{1: 'one', 2: 'two', 3: 'three', 4: 'four'}



Here's another way:
.... def __getitem__(self, i):
.... if i > 4:
.... raise IndexError
.... return (i, 'foo %d' % i)
....{0: 'foo 0', 1: 'foo 1', 2: 'foo 2', 3: 'foo 3', 4: 'foo 4'}



Bonus marks if you can explain why they both work :)

(Hint: consider the "sequence protocol" and the "iterator protocol".)
 
O

Oliver Beattie

Just wondering if it is possible to pass a custom class instance
instance to dict() by way of using methods like you can for iterators
(__iter__, __getitem__ etc.) I see there is no __dict__ -- is there
anything else I can use to achieve this?

Just write a method to return (key, value) pairs, and call that:

...     def __init__(self):
...             self.keys = [1, 2, 3, 4]
...             self.values = ["one", "two", "three", "four"]
...     def generate_tuples(self):
...             for k,v in zip(self.keys, self.values):
...                     yield (k,v)
...>>> p = Parrot()
<generator object at 0xb7d1d78c>>>> dict(p.generate_tuples())

{1: 'one', 2: 'two', 3: 'three', 4: 'four'}

Here's another way:

...     def __getitem__(self, i):
...             if i > 4:
...                     raise IndexError
...             return (i, 'foo %d' % i)
...>>> dict(Foo())

{0: 'foo 0', 1: 'foo 1', 2: 'foo 2', 3: 'foo 3', 4: 'foo 4'}

Bonus marks if you can explain why they both work :)

(Hint: consider the "sequence protocol" and the "iterator protocol".)

Sure, I get what you're saying here and thanks for the advice; but I
don't want the keys as the iterator indices -- They should have custom
names (latitude, longitude and elevation). Is this possible (outside
of the custom method to generate two-tuples?) Sorry to be a pain!

The class looks like the below; I just whipped this up real quick but
it can generate the iterators it should -- just the dictionaries
should be different -- {'latitude': 0.0, 'longitude': 0.0,
'elevation': 0.0} or whatever):

class Coordinates(object):
"""Basic object for storing co-ordinate data."""
latitude = 0.0
longitude = 0.0
elevation = 0.0

def __unicode__(self):
return u'Coordinate (%s, %s, %s)' % (self.latitude, self.longitude,
self.elevation)

def __repr__(self):
return '<Coordinates instance at (%s, %s, %s)>' % (self.latitude,
self.longitude, self.elevation)

def __iter__(self):
return iter((self.latitude, self.longitude, self.elevation))

I guess it's just easier to have a dict() method to this end; just
wondered if there was a more 'Pythonic' way to do this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top