Question

I have a dictionary of objects like so {'name1':oject_instance_1,'name2':oject_instance_2,'name3':oject_instance_3} In my class definition of object, I have defined both the __str__() method and the __repr__() method as follows:

def __str__(self):
    return('{0} pathway containing {1} genes, with a total sequence length of {2}'.format(self.id, len(self.genes), self.length))

def __repr(self):
    return self.__str__    

In case it's important self.id is a string, self.genes is a list, and self.length is an int

The problem is when I go to print this dictionary I get:

{'pid1003': <Pathway.Pathway instance at 0x10169d680>, 'pid1002': <Pathway.Pathway instance at 0x10169d638>, 'pid1001': <Pathway.Pathway instance at 0x10169d5f0>}

but printing in a loop like

for v in dict.values():
    print(v)

works fine.

Any ideas why? Thanks!

Was it helpful?

Solution

Maybe you should implement __repr__, not __repr.

Edit:

And __repr__ should return a String, not a function. So, as noted in the comments, return str(self).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top