This article is part of in the series
Last Updated: Thursday 12th December 2013

In the first part of this series, we looked at the basics of using classes in Python. Now we’ll take a look at some more advanced topics.

Python Class Inheritance

Python classes support inheritance, which lets us take a class definition and extend it. Let's create a new class that inherits (or derives) from the example in part 1:

This defines a class called DerivedFoo that has everything the Foo class has, and also adds a new method called negateVal. Here it is in action:

Inheritance becomes really useful when we re-define (or override) a method that is already defined in the base class:

We can test the class as follows:

The derived class re-defines the printVal method to do something different, and it is this new version that will be used whenever printVal is called. This lets us change the behavior of the class, which is usually what we want (since if we wanted the original behavior, we would just use the original class). Note that the new version of this method calls the old version, and the call is prefixed with the name of the base class (otherwise Python would assume you're calling the new version).

Python offers several functions to help you figure out what class an object is:

  • isinstance checks if an object is an instance of the specified class, or a derived class.

Such as the following:

  • issubclass checks if a class is derived from another class

Such as the following:

Python Class Iterators and Generators

Python's for statement will loop over anything that is iterable, which includes built-in data types such as arrays and dictionaries. For example:

When we define our own classes, we can make them iterable, which will allow them to also work in a for loop. We do this by defining an __iter__ method, which returns an iterator (an object that keeps track of where we are in the loop), and a __next__ method that returns the next available value. Note that the syntax of the next method is different between Python 3.x and Python 2.x. For Python 3.x you must use the __next__ method, whereas for Python 2.x you must use the next method.

Here's a simple example that lets you iterate backwards over a data structure. Here's the class definition:

And here's an example of iterating over the class:

The class tracks two things, the data structure being iterated over, and the next value to be returned. The __iter__ method just returns a reference to the object itself, since this is what’s being used to manage the loop. When Python loops over the object, it repeatedly calls the next method to get the next value, until a StopIteration exception is thrown when there are no more left.

This is a very simple example, but most of it is boiler-plate code (to get each item and track where we're up to in the loop) that will be the same every time we want to create an iterable class. However, Python comes to our rescue yet again and gives us a way to eliminate all of this repetitive administrative code, using generators.

A generator is a special kind of function that returns an iterable object that auto-magically remembers where it's up to in a loop. Here's the same example, done this time using a generator.

The function can be defined as follows (Note: using the yield keyword):

Here's how we can use the generator:

If you've never seen this kind of thing before, it can be really hard to get your head around it, but the easiest way to think of it is to read the backwards function like this:

  • Loop backwards over the value passed in.
  • On each pass, yield the next value i.e. temporarily stop executing the loop and return the next value to the caller. It does whatever it wants with it, then when it calls us again, we resume the loop from where we left off.

Python Classes as Objects

A class is a description of what instances of that class will look like i.e. what methods and member variables they will have. Internally, Python keeps track of each class definition in its own object, which we can modify. This means we can change the definition of a class on the fly, or even create a completely new class at run-time!

Let's start with a simple class definition:

Let's see the usage:

Oops! We got an error, because the class doesn't have a printVal method.

OK, let's add one :-). We can define it as follows:

And we can add the function to the class as follows:

We defined a method called printVal that is stand-alone (i.e. it's defined outside of the class), but it looks like a class method (i.e. takes a self parameter). We then added it to the class definition (Foo.printVal = printVal), which then makes it available as if it had been part of the original class definition.

If we want to remove it, we can do that using the normal del statement:

To create a brand-new class at runtime, we use the type method:

The second parameter to the type call is a list of classes we want to derive from, while the third parameter is a dictionary of methods and member variables that will make up the class definition (you can define them here, or add them on-the-fly as described above).

To understand generators and the yield keyword in Python, checkout the article Python Generators and the yield Keyword.