Hi friends,
My problem definition is as follows:
Creating a class name called "Kangaroo".
1] An __init__ method that initializes an attribute named pouch_contents to an empty list.
2]A method named by creating put_in_pouch that takes an object of any type and adds it to pouch_contents.

The following is the code I have written:

class Kangaroo:
    def __init__(self,pouch=[],pouch_contents=10):
        self.pouch.append(pouch_contents)
        print self.pouch
        
        
    def __add__(self,other):
        if isinstance(other,Kangaroo):
            return self.put_in_pouch(other)
        else:
            return False
        
    def __print__(self):
        print '%d'% (self.pouch)

        
    def put_in_pouch(self,other):
        temp=Kangaroo()
        temp.pouch=self.pouch+other.pouch
        return temp

    

kanga=Kangaroo()
roo=Kangaroo()
print kanga + roo

I am testing the code by creating 2 Kangaroo class objects. But for the surprising fact that the pouch_contents don't get added to the specific Kangaroo object. It adds to a normal list. But it's not what I want. I want to add the pouch_contents for the specific object as it I ultimately would like to add contents off 2 different objects.

Kindly help.

Thank You.

not sure what you mean exactly, but why are you creating a temporary instance of Kangaroo in the put in pouch function?
I thought you meant you wanted roo's pouch added into kanga's pouch?

if thats the case then your code is fine, except you need to define pouch in init.

class Kangaroo:
    def __init__(self, contents=10):
        self.pouch = []
        self.pouch.append(contents)
...
...
...
    def put_in_pouch(self, other)
        self.pouch.extend(other.pouch)

master = Kangaroo('master')
for i in range(10):
    master + Kangaroo(i)

print master.pouch

my result is:

>>> 
['master']
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
['master', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

hope that helps
a1eio

Hi,
Firstly, Thank You.
Secondly,
I have now modified my code :

class Kangaroo:
    def __init__(self,pouch_contents=10):
        self.pouch=[]
        self.pouch.append(pouch_contents)
        print self.pouch
        
        
    def __add__(self,other):
        if isinstance(other,Kangaroo):
            return self.put_in_pouch(other)
        else:
            return False
        
    def __str__(self):
        print self.pouch

        
    def put_in_pouch(self,other):
        self.pouch.extend(other.pouch)
        return self

    

kanga=Kangaroo()
roo=Kangaroo(100)
kanga + roo
print kanga.pouch

I would like to take advantage of the __str___ function.
However, whenever I say print kanga + roo it doesn't gives me an error as to __str__ is returning a non-string type.
I usually use the __str__ special method to print my data directly.

What could probably be the problem?

PS: I am learning Python and hence the mistakes I make are obvious.Sorry !

well, if you want to be able to 'print' the kangaroo instance in your case your trying to print 'kanga' then just make the '__str__' method return a string and that string will get printed:

...
...
    def __str__(self):
        return str(self.pouch)
....
....
kanga = Kangaroo()
roo = Kangaroo()
kanga + roo
print kanga

i get:

>>> 
[10]
[100]
[10, 100]
>>>

hope that helps
a1eio

Hi,
Is there any other method to which a list can be returned ?

__str__ is for String is there a special method for lists ?

i'm not sure, i havn't used the double underscore methods much (if at all) but as far as i am aware, the __str__ method links with the 'print' keyword, so if print object is called then that objects __str__ method would be called and the result (returned value) of that would be displayed if it was a string, otherwise it returns an error.
So i'm not really sure if there's a method to return a list.

I am currently looking through the following page to see what kinda things you can do. You will probably find it useful also, although sometimes the documentation is a bit hard to understand (i think so anyway).

hope that helps
a1eio

i'm not sure, i havn't used the double underscore methods much (if at all) but as far as i am aware, the __str__ method links with the 'print' keyword, so if print object is called then that objects __str__ method would be called and the result (returned value) of that would be displayed if it was a string, otherwise it returns an error.
So i'm not really sure if there's a method to return a list.

I am currently looking through the following page to see what kinda things you can do. You will probably find it useful also, although sometimes the documentation is a bit hard to understand (i think so anyway).

hope that helps
a1eio

Oh yes, about the __str__ method you are right, it does link with print. I was probably asking for a bit more from the method.
By the way,Which page are you looking at ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.