Deelaka
Deelaka

Reputation: 13721

Retrieving data from a class without the str method

I posses a class called Collatz and a function collatz_it which creates an object of the class, I'm trying to generate the number of steps for a number to reach 1 using the collatz conjucture and their corresponding steps till 1 million using a generator

import collatz
values = {}
count = 0
#collatz.collatz_it(n)

def gen():
    n = 0
    x = 0
    while True:
        yield x
        n += 1
        x = collatz.collatz_it(n)

for i in gen():
    count += 1
    values[count] = i
    print values
    if count == 1000000:
        break

As you can see, I generate the amount of steps taken for it to reach 1 using the collatz conjecture for a number given and add it to a dictionary with the corresponding number but when I print out the dictionary values, it's output is awkwardly something like this:

{1: 0}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>, 5: <collatz.Collatz instance at 0x01DCDEB8>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>, 5: <collatz.Collatz instance at 0x01DCDEB8>, 6: <collatz.Collatz instance at 0x01DCDE90>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>, 5: <collatz.Collatz instance at 0x01DCDEB8>, 6: <collatz.Collatz instance at 0x01DCDE90>, 7: <collatz.Collatz instance at 0x01DE8940>}

If I print print i instead of print values I get the required output, this is basically because the print statement triggers the __str__ method in the class

Isn't there any way I could add the actual steps to the dictionary without entering <collatz.Collatz instance at 0x01DCDFA8> , Is there any sort of method of retrieving data as from a __str__ method so that my dictionary looks something like this:

{1: 0}
{1: 0, 2: 1}
{1: 0, 2: 1, 3: 7}

Upvotes: 1

Views: 93

Answers (3)

OregonTrail
OregonTrail

Reputation: 9049

Store the value that you want on this line:

values[count] = i

Do something like

values[count] = i.value

or

values[count] = str(i)

In the last case this assumed that you have written a __str__ method for the class:

class Collatz:
...
def __str__(self):
    return str(self.value)

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124848

The default representation of any Python container is to use the repr() output of the contents, not str().

The solution would be for you to either give the collatz.Collatz() instance a __repr__ method (you can monkey-patch that in), or to use a subclass of dict that uses str() instead of repr() when displaying the contents.

Monkey-patching in a __repr__ could be as simple as:

collatz.Collatz.__repr__ = collatz.Collatz.__str__

Of course, if this is your own code, just define a __repr__ method in the class body itself.

Upvotes: 3

Marcin
Marcin

Reputation: 49886

Use a class which inherits from dict and has its own __repr__ which does what you want.

Upvotes: 0

Related Questions