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’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"""

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
  • Tutorial – Classes
  • Python classes FAQ
  • 5 thoughts on “Classes”

    1. My brother rndemmeoced I would possibly like this web site. He was totally right. This post actually made my day. You cann’t imagine just how much time I had spent for this information! Thanks!

    2. Comme le dit Vincent ci dessus, la vente a decouvert a un interet dans la limitation des bulles speculatives.Que l’on souhaite revoir le principe et les encadrer c’est une chose mais je suis etonne de vous lire affirmer qu’ils n’ont aucune valeur sociale.

    3. Keri – I had a Bishop who would not put a woman and man together (who were not married) into the nursery or primary for fear they will “hook up”. I was shocked that we don’t trust our adults with each other. I can understand two teachers with kids, but not to allow an adult male and adult female (who are not married) to serve together in a calling…

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>