Classes


If you are familiar with Object Oriented programming you probably already know what classes are. If you are unfamiliar with the idea of classes in programming I suggest you give the class topic a read over at wikipedia, it should familiarize you with the idea of a class.

Once you know what a class is you should read the tutorial over a python.org’s section on classes in Python. I’ll hi-light some of the main points here, but for an in-depth understanding of the way that classes work in Python, it’s a must read.

First off we’ll take a look at the syntax of a class:

class classname:


The term class starts the definition of a class and is then followed by the name of the class. If the class is derived from another class the syntax is as following:

class classname(baseclass1, baseclass1, ..., baseclassN):

Classes can have data members and function members:

class TestClass:
    i = 10;
    def PrintI(self):
        print self.i

One of the things you’ll notice about the function definition PrintI within the above class is the strange use of the self variable. What self is, is actually a reference to the class itself, which is why you see that in order to reference the variable i in the PrintI function, self.i is used rather then simply i. If i was simply used then i would become a local variable in the function.

You can use any term as the first parameters in functions within classes, it does not have to be self, however self has become somewhat of a standard so it’s recommended that use it.

So now you can reference the above class in the following way:

"""Create an Instance of the class"""
tc = TestClass();
"""Print out the value of the i variable"""
print tc.i
"""Call the PrintI function to print out the 
value of the i variable"""
tc.PrintI()

The above code would result in (as you might have guessed) the value 10 being written out twice. There are many situations where you will want to construct your class with some default settings, or set some values. This is done using the __init__ function, which is automatically called ever time you construct, or create an instance of, a class. Sin instead of setting i, the way that we did above, we should probably do something like:

class TestClass:
    def __init__(self,value):
        self.i = value
    def PrintI(self):
        print self.i

Where value is an extra parameter was pass to the construction of TestClass:

"""Create an Instance of the class"""
tc = TestClass(23);

Classes also support docstrings in the same way that functions do. The docstring can be references in code using the __doc__ attribute.

That’s about it for basic work with classes, for more information I really recommend reading the section on classes in the Python tutorial or any othe the lings below:

  • Itroduction to Python – Classes
  • Dive into Python – Defining Classes
  • Learning to Program – Object Oriented Programming
  • Pythong.org Tutorial – Classes
  • Python classes FAQ
  • selsine

    del.icio.us del.icio.us

    2 Responses to “Classes”

    1. learning python » Blog Archive » RSS reader - Part Three - Generator Class
      Says:

      [...] learning python one man’s journey into python… « Classes [...]

    2. Ian Ozsvald
      Says:

      If you want to learn more about Python Classes, you might like our three-part video introduction over at ShowMeDo.com by Jerol Harrington. These three videos are a part of our growing collection of Python ShowMeDo Videos.

    Leave a Reply

     

    Popular Posts