First OO Python Program

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 3
Reputation: smerny is an unknown quantity at this point 
Solved Threads: 0
smerny smerny is offline Offline
Newbie Poster

First OO Python Program

 
0
  #1
Sep 25th, 2009
Hello, I'm trying to learn how to create a very simple class just to get the basics of OOP in python.

I figure just to learn I'll start with a class where I can create an object and use a method to update a string associated with that object and another method to get that string. So I should be able to do this:

  1. myObject = MyClass("BlahBlah")
  2. print myObject.getString()
  3. # outputs "BlahBlah"
  4. myString = "More Text"
  5. myObject.addText(myString)
  6. print myObject.getString()
  7. # outputs "BlahBlahMore Text"
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 339
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 61
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: First OO Python Program

 
1
  #2
Sep 25th, 2009
OK, here's a very simple class and some code to test it, based on what you posted previously!
  1. # here's a simple class
  2. class MyClass:
  3. # class constructor/initialisation
  4. # if no string is passed, defaults to an empty string
  5. def __init__(self, aString=""):
  6. self.myString = aString
  7.  
  8. # addText function
  9. def addText(self, appendedText):
  10. self.myString += appendedText
  11.  
  12. # getString function - returns the current string
  13. def getString(self):
  14. return self.myString
  15.  
  16. # Now lets test the class...
  17. print "Testing the class [passing a literal string to the constructor]"
  18. # Create an instance of the class, passing some text
  19. myObject = MyClass("Blah blah blah...")
  20. print myObject.getString() # Lets see what we've got
  21.  
  22. # now lets add some text by passing a literal string to addText...
  23. myObject.addText("More text...")
  24. print myObject.getString() # Now what do we have?
  25.  
  26. # now lets create a string object and pass it to addText...
  27. anotherString = "Yet more text!"
  28. myObject.addText(anotherString)
  29. print myObject.getString() # Now what've we got?
  30.  
  31. print "\nTesting the class [passing no parameters!]"
  32. myOtherObject = MyClass()
  33. print myOtherObject.getString() # This should print a blank line
  34.  
  35. # now lets pass addText the string object we created earlier!
  36. myOtherObject.addText(anotherString)
  37. print myOtherObject.getString() # now what have we got?
The addText and getString functions speak for themselves..They're pretty straightforward.

The __init__ function is the important bit here, this is what gets called when you instantiate the class. You perform any initialisation for your object here.

The other thing of note is 'self', which is kinda like the 'this' pointer in C/C++/Actionscript (and Java?.. it's been so long since I saw any Java code I can't remember!)...Either way, it's equivalent to the 'this' pointer in other object oriented programming languages and refers to the active instance of the class.

So for any of the myObject class member function calls:
e.g.
  1. myObject = MyClass("Whatever")
  2. myObject.addText()
  3. myObject.getString()

The self pointer in the class definition points to myObject. Likewise for myOtherObject, the self pointer points to myOtherObject.

Not sure if I'm explaining this too clearly, but I hope you get the point. Anyway, that's classes. It doesn't really get much more complicated than that really!

Cheers for now,
Jas.
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: smerny is an unknown quantity at this point 
Solved Threads: 0
smerny smerny is offline Offline
Newbie Poster

Re: First OO Python Program

 
0
  #3
Sep 25th, 2009
wow, very descriptive, thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: First OO Python Program

 
0
  #4
Sep 25th, 2009
Originally Posted by smerny View Post
wow, very descriptive, thanks a lot
I agree! Very nice of our friend JasonHippy to enlighten us so thoroughly!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 339
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 61
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: First OO Python Program

 
0
  #5
Sep 25th, 2009
hmm, why thank you very much...

Is the problem solved??
Last edited by JasonHippy; Sep 25th, 2009 at 10:26 pm.
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC