Python Programming/Classes

Classes are a way of aggregating similar data and functions. A class is basically a scope inside which various code (especially function definitions) is executed, and the locals to this scope become attributes of the class, and of any objects constructed by this class. An object constructed by a class is called an instance of that class.

Overview
Classes in Python at a glance:

Defining a Class
To define a class, use the following format:

The capitalization in this class definition is the convention, but is not required by the language. It's usually good to add at least a short explanation of what your class is supposed to do. The pass statement in the code above is just to say to the python interpreter just go on and do nothing. You can remove it as soon as you are adding your first statement.

Instance Construction
The class is a callable object that constructs an instance of the class when called. Let's say we create a class Foo.

To construct an instance of the class, Foo, "call" the class object:

This constructs an instance of class Foo and creates a reference to it in f.

Class Members
In order to access the member of an instance of a class, use the syntax. It is also possible to access the members of the class definition with.

Methods
A method is a function within a class. The first argument (methods must always take at least one argument) is always the instance of the class on which the function is invoked. For example

If this code were executed, nothing would happen, at least until an instance of Foo were constructed, and then bar were called on that instance.

Why a mandatory argument?
In a normal function, if you were to set a variable, such as, you could not access the test variable. Typing  would say it is not defined. This is true in class functions unless they use the  variable.

Basically, in the previous example, if we were to remove self.x, function bar could not do anything because it could not access x. The x in setx would disappear. The self argument saves the variable into the class's "shared variables" database.

Why self?
You do not need to use self. However, it is a norm to use self.

Invoking Methods
Calling a method is much like calling a function, but instead of passing the instance as the first parameter like the list of formal parameters suggests, use the function as an attribute of the instance.

This will output 5

It is possible to call the method on an arbitrary object, by using it as an attribute of the defining class instead of an instance of that class, like so:

This will have the same output.

Dynamic Class Structure
As shown by the method setx above, the members of a Python class can change during runtime, not just their values, unlike classes in languages like C++ or Java. We can even delete f.x after running the code above.

Traceback (most recent call last): File " ", line 1, in ? File " ", line 5, in bar AttributeError: Foo instance has no attribute 'x'

Another effect of this is that we can change the definition of the Foo class during program execution. In the code below, we create a member of the Foo class definition named y. If we then create a new instance of Foo, it will now have this new member.

Viewing Class Dictionaries
At the heart of all this is a dictionary that can be accessed by "vars(ClassName)"

At first, this output makes no sense. We just saw that g had the member y, so why isn't it in the member dictionary? If you remember, though, we put y in the class definition, Foo, not g.

And there we have all the members of the Foo class definition. When Python checks for g.member, it first checks g's vars dictionary for "member," then Foo. If we create a new member of g, it will be added to g's dictionary, but not Foo's.

Note that if we now assign a value to g.y, we are not assigning that value to Foo.y. Foo.y will still be 10, but g.y will now override Foo.y

Sure enough, if we check the values:

Note that f.y will also be 10, as Python won't find 'y' in vars(f), so it will get the value of 'y' from vars(Foo).

Some may have also noticed that the methods in Foo appear in the class dictionary along with the x and y. If you remember from the section on lambda functions, we can treat functions just like variables. This means that we can assign methods to a class during runtime in the same way we assigned variables. If you do this, though, remember that if we call a method of a class instance, the first parameter passed to the method will always be the class instance itself.

Changing Class Dictionaries
We can also access the members dictionary of a class using the __dict__ member of the class.

If we add, remove, or change key-value pairs from g.__dict__, this has the same effect as if we had made those changes to the members of g.

Why use classes?
Classes are special due to the fact once an instance is made, the instance is independent of all other instances. I could have two instances, each with a different x value, and they will not affect the other's x.

and  will print different values.

New Style Classes
New style classes were introduced in python 2.2. A new-style class is a class that has a built-in as its base, most commonly object. At a low level, a major difference between old and new classes is their type. Old class instances were all of type. New style class instances will return the same thing as x.__class__ for their type. This puts user defined classes on a level playing field with built-ins. Old/Classic classes are slated to disappear in Python 3. With this in mind all development should use new style classes. New Style classes also add constructs like properties and static methods familiar to Java programmers.

Old/Classic Class

New Style Class

Properties
Properties are attributes with getter and setter methods.

and since Python 2.6, with @property decorator

Static Methods
Static methods in Python are just like their counterparts in C++ or Java. Static methods have no "self" argument and don't require you to instantiate the class before using them. They can be defined using staticmethod

They can also be defined using the function decorator @staticmethod.

Inheritance
Like all object oriented languages, Python provides support for inheritance. Inheritance is a simple concept by which a class can extend the facilities of another class, or in Python's case, multiple other classes. Use the following format for this:

ClassName is what is known as the derived class, that is, derived from the base classes. The derived class will then have all the members of its base classes. If a method is defined in the derived class and in the base class, the member in the derived class will override the one in the base class. In order to use the method defined in the base class, it is necessary to call the method as an attribute on the defining class, as in Foo.setx(f,5) above:

Once again, we can see what's going on under the hood by looking at the class dictionaries.

When we call g.x, it first looks in the vars(g) dictionary, as usual. Also as above, it checks vars(Bar) next, since g is an instance of Bar. However, thanks to inheritance, Python will check vars(Foo) if it doesn't find x in vars(Bar).

Multiple inheritance
As shown in section, a class can be derived from multiple classes:

A tricky part about multiple inheritance is method resolution: upon a method call, if the method name is available from multiple base classes or their base classes, which base class method should be called.

The method resolution order depends on whether the class is an old-style class or a new-style class. For old-style classes, derived classes are considered from left to right, and base classes of base classes are considered before moving to the right. Thus, above, BaseClass1 is considered first, and if method is not found there, the base classes of BaseClass1 are considered. If that fails, BaseClass2 is considered, then its base classes, and so on. For new-style classes, see the Python documentation online.

Links:
 * 9.5.1. Multiple Inheritance, docs.python.org
 * The Python 2.3 Method Resolution Order, python.org

Special Methods
There are a number of methods which have reserved names which are used for special purposes like mimicking numerical or container operations, among other things. All of these names begin and end with two underscores. It is convention that methods beginning with a single underscore are 'private' to the scope they are introduced within.

__init__
One of these purposes is constructing an instance, and the special name for this is '__init__'. __init__ is called before an instance is returned (it is not necessary to return the instance manually). As an example,

outputs A.__init__

__init__ can take arguments, in which case it is necessary to pass arguments to the class in order to create an instance. For example,

outputs Hi! Here is an example showing the difference between using __init__ and not using __init__:

outputs Hi! Hi!

__del__
Similarly, '__del__' is called when an instance is destroyed; e.g. when it is no longer referenced.

__enter__ and __exit__
These methods are also a constructor and a destructor but they're only executed when the class is instantiated with. Example:

init enter exit del

__new__
constructor.

Representation
{| style="width:100%;"
 * - style="vertical-align: top;"
 * style="width:75%;" |

__str__
Converting an object to a string, as with the print statement or with the str conversion function, can be overridden by overriding __str__. Usually, __str__ returns a formatted version of the objects content. This will NOT usually be something that can be executed.

For example:

outputs apple

__repr__
This function is much like __str__. If __str__ is not present but this one is, this function's output is used instead for printing. __repr__ is used to return a representation of the object in string form. In general, it can be executed to get back the original object.

For example:

outputs (note the difference: it may not be necessary to put it inside a print, however in Python 2.7 it does) Bar('apple')
 * style="width:25%;" align="right" |
 * }

Attributes
{| style="width:100%;"
 * - style="vertical-align: top;"
 * style="width:75%;" |

__setattr__
This is the function which is in charge of setting attributes of a class. It is provided with the name and value of the variables being assigned. Each class, of course, comes with a default __setattr__ which simply sets the value of the variable, but we can override it.

Traceback (most recent call last): File " ", line 1, in ? AttributeError: Unchangable instance has no attribute 'x'

__getattr___
Similar to __setattr__, except this function is called when we try to access a class member, and the default simply returns the value.

__delattr__
This function is called to delete an attribute.


 * style="width:25%;" align="right" |
 * }

Operator Overloading
Operator overloading allows us to use the built-in Python syntax and operators to call functions which we define.

Programming Practices
The flexibility of python classes means that classes can adopt a varied set of behaviors. For the sake of understandability, however, it's best to use many of Python's tools sparingly. Try to declare all methods in the class definition, and always use the. syntax instead of __dict__ whenever possible. Look at classes in C++ and Java to see what most programmers will expect from a class.

Encapsulation
Since all python members of a python class are accessible by functions/methods outside the class, there is no way to enforce encapsulation short of overriding __getattr__, __setattr__ and __delattr__. General practice, however, is for the creator of a class or module to simply trust that users will use only the intended interface and avoid limiting access to the workings of the module for the sake of users who do need to access it. When using parts of a class or module other than the intended interface, keep in mind that the those parts may change in later versions of the module, and you may even cause errors or undefined behaviors in the module, since encapsulation is private.

Doc Strings
When defining a class, it is convention to document the class using a string literal at the start of the class definition. This string will then be placed in the __doc__ attribute of the class definition.

Docstrings are a very useful way to document your code. Even if you never write a single piece of separate documentation (and let's admit it, doing so is the lowest priority for many coders), including informative docstrings in your classes will go a long way toward making them usable.

Several tools exist for turning the docstrings in Python code into readable API documentation, e.g., EpyDoc.

Don't just stop at documenting the class definition, either. Each method in the class should have its own docstring as well. Note that the docstring for the method explode in the example class Documented above has a fairly lengthy docstring that spans several lines. Its formatting is in accordance with the style suggestions of Python's creator, Guido van Rossum in PEP 8.

To a class
It is fairly easy to add methods to a class at runtime. Lets assume that we have a class called Spam and a function cook. We want to be able to use the function cook on all instances of the class Spam:

This will output cooking 5 eggs

To an instance of a class
It is a bit more tricky to add methods to an instance of a class that has already been created. Lets assume again that we have a class called Spam and we have already created eggs. But then we notice that we wanted to cook those eggs, but we do not want to create a new instance but rather use the already created one:

Now we can cook our eggs and the last statement will output: cooking 5 eggs

Using a function
We can also write a function that will make the process of adding methods to an instance of a class easier.

All we now need to do is call the attach_method with the arguments of the function we want to attach, the instance we want to attach it to and the class the instance is derived from. Thus our function call might look like this:

Note that in the function add_method we cannot write instance.fxn = f since this would add a function called fxn to the instance.