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:

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!

# 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 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.

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.

Comments
now that is class!

wow, very descriptive, thanks a lot

I agree! Very nice of our friend JasonHippy to enlighten us so thoroughly!

This article has been dead for over six months. Start a new discussion instead.