PDA

View Full Version : [Tutorial] Object-oriented programming and classes in Python


Strike
05-29-2002, 11:49 AM
First of all, if you haven't read it yet - read the chapter on classes in the Python tutorial here (http://www.python.org/doc/current/tut/node11.html).

This tutorial assumes a cursory understanding of OOP (object-oriented programming). You should be familiar with the terms: object, inheritance, interface, method, instance, constructor, and some others. If you don't know them all solidly, that's okay, you should be able to infer the meaning from the text for some of them.

Anyway, classes in Python are very easy to declare, you simply declare them as if they were any other block of things, by putting a sort of "title line" on it, and then an indented block containing everything that belongs in the class, like so:

class myClass:
member_int = 1
member_string = "hi"

def myMethod(self):
print "This does stuff"
return

def myOtherMethod(self, foo):
print "This does stuff too - %s" % foo
return

That creates a class named myClass. To create an instance of an object of myClass, you simply do:

foo = myClass()

And if you run this example through the interpreter (beware the spaces in the class definition between the methods while in the interpreter, as they won't work), you can see that you do indeed get an object that is an instance of "myClass", defined in the scope of "__main__":

>>> blah = myClass()
>>> blah
<__main__.myClass instance at 0x810e7d4>

Now, member and method access for classes is basically the same as every other OO language I have ever seen. Python uses the . to access methods/members of a class instance. So, to see what member_int for blah is, I just do:

>>> blah.member_int
1

And to run myMethod for this instance, I do:

>>> blah.myMethod()
This does stuff

Now, you may be wondering what that "self" in the two method argument lists are, and why myMethod has that one argument, but doesn't get passed any (normally Python bitches about mismatched argument list lengths). Well, self is something you either love or hate about Python OOP, and I think it's pretty cool personally. Every class method must have a self argument as the first argument in the list. This is explicit self-referencing. So, if you wanted to use the member_int of a given instance in one of the methods, you would add a method that looks something like this:

def myIntPrinter(self):
print "My member_int - %d" % self.member_int
return

Here's an example using the new, re-defined myClass to include that method:

>>> blah = myClass()
>>> blah.myIntPrinter()
My member_int - 1
>>> blah.member_int
1
>>> blah.member_int = 100
>>> blah.myIntPrinter()
My member_int - 100

For those of you familiar with C++, this is similar to the this pointer you have access to.

One more thing to discuss in this tutorial: special methods.

SPECIAL METHODS
What are they? Well, it's a way of customizing your class so that they work with some of the standard builtin functions (if you are wondering what those are, run dir(__builtins__) in the interpreter). For example, if you make your own complex number class, you might want to let the comparison operators work on them without having to use some stupid notation like:

if foo.norm() > bar.norm():
when you will ALWAYS be comparing the norms and the norms alone. Instead, you'd probably prefer to do:

if foo > bar:
Which is more readable, and easier to type.

Well, Python allows for this through what it calls "customization" methods. The language reference discusses them in this chapter (http://www.python.org/doc/current/ref/specialnames.html) - your reference to see just how much you can customize Python classes. For the example mentioned above, we would use the __gt__ method ("gt" meaning "greater than"). All special methods are intended for class use only, and any method that is intended for class use only should have __ at the front of it (special methods just also have that after them).

I'm not going to go more into special methods here (I can write an entire tutorial on this later if you wish), as this is getting lengthy.

Actually, one special method of note that is incredibly useful is __init__. This is the constructor function of your class.

inkedmn
05-29-2002, 12:16 PM
well done my friend :)


/me sticky-fies this thread

Benny
05-31-2002, 02:38 AM
Awesome!

Thanks Strike!!

Bradmont
05-31-2002, 03:29 AM
how long do sticky threads stay sticky?

Strike
05-31-2002, 08:47 AM
Originally posted by Bradmont
how long do sticky threads stay sticky?
I'm willing to wager that the answer is - until they are un-stuck

inkedmn
05-31-2002, 01:03 PM
yeah, i'd have to agree. when we "stick" a thread, we don't have any time option or anything...

it's either stuck or it ain't :)

kmj
05-31-2002, 03:28 PM
You'll notice the C forum now has a "tutorials" sub-forum. We plan to have a similar sub for each language. This seems to me like the best way to do it.