Please support our Python advertiser: Programming Forums
Views: 630 | Replies: 7
![]() |
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Rep Power: 1
Solved Threads: 0
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:
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.
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:
Python Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
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.
my result is:
hope that helps
a1eio
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.
python Syntax (Toggle Plain Text)
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
>>> ['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
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Rep Power: 1
Solved Threads: 0
Hi,
Firstly, Thank You.
Secondly,
I have now modified my code :
I would like to take advantage of the __str___ function.
However, whenever I say
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 !
Firstly, Thank You.
Secondly,
I have now modified my code :
Python Syntax (Toggle Plain Text)
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 !
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
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:
i get:
hope that helps
a1eio
python Syntax (Toggle Plain Text)
... ... 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
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
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
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
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
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 ?
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
hahahaha!!!! i was typing away and hit enter without even pasting the link in 
terribly sorry
http://docs.python.org/ref/customization.html

terribly sorry
http://docs.python.org/ref/customization.html
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode