0

I'm trying to append to a list an object from the given value(amount) up to one. But the problem with what I have right now it that the Coin object can't be interpreted as an integer. Is there a workaround to the add_to_table method in order to achieve what is expected?

class Test:

    def __init__(self, table=[]):
        """(Test, int) -> NoneType
        """
        self.table = [(0, []), (1, []), (2, [])]

    def add_to_table(self, amount):
        """(Test, int) -> NoneType

        Adds to the first table Coin(amount) to Coin(1)

        ex.

        [(0, [Coin(3), Coin(2), Coin(1)]), (1, []), (2, [])]

        """
        self.table[0][1].extend(reversed(range(Coin(1), Coin(amount + 1))))

class Coin:

    def __init__(self, length):
        """(Coin, int) -> NoneType
        """
        self.length = length

Expected output:

t1 = Test()
t1.table
[(0, []), (1, []), (2, [])]
t1.add_to_table(3)
t1.table
[(0, [Coin(3), Coin(2), Coin(1)]), (1, []), (2, [])]
Sc4r
  • 700
  • 1
  • 10
  • 15
  • Aside: even though you're not using the `table` parameter in `__init__` right now, setting the default like `table=[]` is going to get you into trouble down the road. See [here](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) for why you generally don't want to use mutable default arguments. – DSM Feb 01 '14 at 19:09

3 Answers3

2

To get what you want, two changes appear necessary:

class Test:

    def __init__(self, table=[]):
        """(Test, int) -> NoneType
        """
        self.table = [(0, []), (1, []), (2, [])]

    def add_to_table(self, amount):
        """(Test, int) -> NoneType

        Adds to the first table Coin(amount) to Coin(1)

        ex.

        [(0, [Coin(3), Coin(2), Coin(1)]), (1, []), (2, [])]

        """
        self.table[0][1].extend([Coin(n) for n in range(amount, 0, -1)])

class Coin:

    def __init__(self, length):
        """(Coin, int) -> NoneType
        """
        self.length = length

    def __repr__(self):
        return 'Coin(%s)' % (self.length,)

The first change is that, to generate the range of values for Coin, the add_to_table method above uses list comprehension: [Coin(n) for n in range(amount, 0, -1)]. The second change is because you wanted the Coin list to display as [Coin(3), Coin(2), Coin(1)]. The method __repr__ controls how a class is displayed. So this method was added to Coin. With these two changes, the above yields:

>>> t1 = Test()
>>> t1.table
[(0, []), (1, []), (2, [])]
>>> t1.add_to_table(3)
>>> t1.table
[(0, [Coin(3), Coin(2), Coin(1)]), (1, []), (2, [])]
John1024
  • 109,961
  • 14
  • 137
  • 171
0

try this:

def add_to_table(self, amount):
    for i in range(amount, 0, -1):
        self.table[0][1].append(Coin(i))
Jerry_Y
  • 1,724
  • 12
  • 9
  • 1
    Please provide an explanation of what your code is doing and how you came up with it to solve the issue. – helion3 Feb 01 '14 at 19:47
0

Its not clear if you want the table to have the actual Coin objects, or the values of the objects; here I'm assuming its the values.

You need to have a method that returns the value of your Coin objects. If you just add them to a list a special representation of your object (returned by the __repr__ method) is used:

>>> class Foo(object):
...     pass
... 
>>> [Foo(),Foo()]
[<__main__.Foo object at 0x20b44d0>, <__main__.Foo object at 0x20b4510>]
>>> class Foo(object):
...     def __repr__(self):
...         return "I am a Foo object"
... 
>>> [Foo(), Foo()]
[I am a Foo object, I am a Foo object]

However, the __repr__ method should be used to return a string representation of the object, its not for returning meaningful values, in other words, its not the solution here.

You need to have a method in Coin to return the value of the coin, then use that value when you pass it to Table:

class Coin(object):
    ''' A coin object with a length '''

    def __init__(self, length):
        self._length = length

    @property
    def length(self):
        return self._length

Now, you need to do this:

coins = [Coin(1), Coin(2)]
table = Test()
for coin in coins:
    table.add_to_table(coin.length)

I am using the @property decorator to make life easier.

You still have to make sure that the coins were created with integers and not strings, in other words Coin('1') will work perfectly fine, but your add_to_table method will raise an exception.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284