| | |
First OO Python Program
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 3
Reputation:
Solved Threads: 0
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:
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)
myObject = MyClass("BlahBlah") print myObject.getString() # outputs "BlahBlah" myString = "More Text" myObject.addText(myString) print myObject.getString() # outputs "BlahBlahMore Text"
OK, here's a very simple class and some code to test it, based on what you posted previously!
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.
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.
PYTHON Syntax (Toggle Plain Text)
# here's a simple class class MyClass: # class constructor/initialisation # if no string is passed, defaults to an empty string def __init__(self, aString=""): self.myString = aString # addText function def addText(self, appendedText): self.myString += appendedText # getString function - returns the current string def getString(self): return self.myString # Now lets test the class... print "Testing the class [passing a literal string to the constructor]" # Create an instance of the class, passing some text myObject = MyClass("Blah blah blah...") print myObject.getString() # Lets see what we've got # now lets add some text by passing a literal string to addText... myObject.addText("More text...") print myObject.getString() # Now what do we have? # now lets create a string object and pass it to addText... anotherString = "Yet more text!" myObject.addText(anotherString) print myObject.getString() # Now what've we got? print "\nTesting the class [passing no parameters!]" myOtherObject = MyClass() print myOtherObject.getString() # This should print a blank line # now lets pass addText the string object we created earlier! myOtherObject.addText(anotherString) print myOtherObject.getString() # now what have we got?
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)
myObject = MyClass("Whatever") myObject.addText() 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....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
hmm, why thank you very much...
Is the problem solved??

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....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
![]() |
Similar Threads
- My python program/function! (Python)
- how to import other system`s python program into my application (Python)
- How to get PDF file as output from the python program to give a print (Python)
- Word Jumble python program (Python)
- Enable/Disable port through Python program (Python)
- Urgent : Question on editing a python program .... (Python)
- running a process from a python program (Python)
Other Threads in the Python Forum
- Previous Thread: Caeser cipher code breaker help (homework)
- Next Thread: Automatic Downloader from URL
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied advanced application argv beginner change code color command csv def dictionary dynamic edit editing enter event examples excel file float format ftp function google gui homework import inches input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random recursion recursive remote return reverse scrolledtext session simple skinning smtp sprite ssh stderr string strings strip syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip windows wxpython






