943,587 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 457
  • Python RSS
Sep 25th, 2009
0

First OO Python Program

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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"
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smerny is offline Offline
3 posts
since Sep 2009
Sep 25th, 2009
1

Re: First OO Python Program

OK, here's a very simple class and some code to test it, based on what you posted previously!
PYTHON Syntax (Toggle Plain Text)
  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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Sep 25th, 2009
0

Re: First OO Python Program

wow, very descriptive, thanks a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smerny is offline Offline
3 posts
since Sep 2009
Sep 25th, 2009
0

Re: First OO Python Program

Click to Expand / Collapse  Quote originally posted by smerny ...
wow, very descriptive, thanks a lot
I agree! Very nice of our friend JasonHippy to enlighten us so thoroughly!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 25th, 2009
0

Re: First OO Python Program

hmm, why thank you very much...

Is the problem solved??
Last edited by JasonHippy; Sep 25th, 2009 at 10:26 pm.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Caeser cipher code breaker help (homework)
Next Thread in Python Forum Timeline: Automatic Downloader from URL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC