RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 630 | Replies: 7
Reply
Join Date: Mar 2008
Posts: 12
Reputation: mcenley is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mcenley mcenley is offline Offline
Newbie Poster

[Program Query]Classes and Methods

  #1  
Apr 2nd, 2008
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:
  1. class Kangaroo:
  2. def __init__(self,pouch=[],pouch_contents=10):
  3. self.pouch.append(pouch_contents)
  4. print self.pouch
  5.  
  6.  
  7. def __add__(self,other):
  8. if isinstance(other,Kangaroo):
  9. return self.put_in_pouch(other)
  10. else:
  11. return False
  12.  
  13. def __print__(self):
  14. print '%d'% (self.pouch)
  15.  
  16.  
  17. def put_in_pouch(self,other):
  18. temp=Kangaroo()
  19. temp.pouch=self.pouch+other.pouch
  20. return temp
  21.  
  22.  
  23.  
  24. kanga=Kangaroo()
  25. roo=Kangaroo()
  26. 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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: [Program Query]Classes and Methods

  #2  
Apr 2nd, 2008
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.
  1. class Kangaroo:
  2. def __init__(self, contents=10):
  3. self.pouch = []
  4. self.pouch.append(contents)
  5. ...
  6. ...
  7. ...
  8. def put_in_pouch(self, other)
  9. self.pouch.extend(other.pouch)
  10.  
  11. master = Kangaroo('master')
  12. for i in range(10):
  13. master + Kangaroo(i)
  14.  
  15. 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
Reply With Quote  
Join Date: Mar 2008
Posts: 12
Reputation: mcenley is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mcenley mcenley is offline Offline
Newbie Poster

Re: [Program Query]Classes and Methods

  #3  
Apr 2nd, 2008
Hi,
Firstly, Thank You.
Secondly,
I have now modified my code :
  1. class Kangaroo:
  2. def __init__(self,pouch_contents=10):
  3. self.pouch=[]
  4. self.pouch.append(pouch_contents)
  5. print self.pouch
  6.  
  7.  
  8. def __add__(self,other):
  9. if isinstance(other,Kangaroo):
  10. return self.put_in_pouch(other)
  11. else:
  12. return False
  13.  
  14. def __str__(self):
  15. print self.pouch
  16.  
  17.  
  18. def put_in_pouch(self,other):
  19. self.pouch.extend(other.pouch)
  20. return self
  21.  
  22.  
  23.  
  24. kanga=Kangaroo()
  25. roo=Kangaroo(100)
  26. kanga + roo
  27. 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 !
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: [Program Query]Classes and Methods

  #4  
Apr 2nd, 2008
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:

  1. ...
  2. ...
  3. def __str__(self):
  4. return str(self.pouch)
  5. ....
  6. ....
  7. kanga = Kangaroo()
  8. roo = Kangaroo()
  9. kanga + roo
  10. print kanga

i get:
>>> 
[10]
[100]
[10, 100]
>>> 

hope that helps
a1eio
Reply With Quote  
Join Date: Mar 2008
Posts: 12
Reputation: mcenley is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mcenley mcenley is offline Offline
Newbie Poster

Re: [Program Query]Classes and Methods

  #5  
Apr 2nd, 2008
Hi,
Is there any other method to which a list can be returned ?

__str__ is for String is there a special method for lists ?
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: [Program Query]Classes and Methods

  #6  
Apr 2nd, 2008
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
Reply With Quote  
Join Date: Mar 2008
Posts: 12
Reputation: mcenley is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mcenley mcenley is offline Offline
Newbie Poster

Re: [Program Query]Classes and Methods

  #7  
Apr 2nd, 2008
Originally Posted by a1eio View Post
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 ?
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: [Program Query]Classes and Methods

  #8  
Apr 2nd, 2008
hahahaha!!!! i was typing away and hit enter without even pasting the link in
terribly sorry

http://docs.python.org/ref/customization.html
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:27 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC