18.2. What are objects and classes? An example

Our example is a class that handles DNA sequences.

18.2.1. Objects description

Let us first start by describing our DNA object. Figure 18.1 shows an internal and schematic representation of such an object named s1. The object is actually represented in this diagram as a box enclosing attributes: the name of the sequence (for instance 'seq1') is stored in a name attribute, and the sequence characters (e.g 'aaacaacttcgtaagtata') is stored in a seq attribute. Attributes are the same as variables, except that they are associated to an object.

Figure 18.1. A DNA object

18.2.2. Methods

As it is described so far, the DNA object is just a kind of record to group data. There are other ways of grouping data in Python: you can either use a list, or a dictionary:

>>> seq_record = {'name': 'seq1', 'seq': 'aaacaacttcgtaagtata'}
>>> seq_record['name']
'seq1'
	  
but an object is actually more than just that. As shown in Figure 18.2 (and as stated in Section 6.1), an object is indeed more than just a value or a record of values: it can "do" things, and it "knows" what it can do, or. In other words, it has an associated behaviour. This behaviour is described by functions that are associated to it, and these functions are called methods. For instance, our DNA object can perform operations: it can translate its sequence or it can compute its GC percent by calling the gc function:
>>> s1.gc()
0.26
	  
So, available operations are an important aspect of objects, that we need to add in our representation. In Figure 18.2, methods are represented as "counters", as at the post office.

Figure 18.2. Representation showing object's methods as counters

So, with objects, not only do we have a convenient way to group data that are related, but we also have a way to group data and their related operations in the same place.

Object

An object is a construction to group values and operations on these values.

18.2.3. Classes

Now, how do you define objects, such as our DNA object, with their attributes and methods?

In object-oriented programming, you define objects by defining a class for them.

Class

A class is an object maker: it contains all the statements needed to create an object, its attributes, as well as the statements to describe the operations that the object will be able to perform.

The term class can be somewhat misleading, for it also refers to something belonging to a classification. In object-oriented programming, a class can indeed also be a member of a set of classes organized in a hierarchy. This aspect will be introduced later (see Section 19.4), through the concept of inheritance. But for now, we will use the term class as just a maker of objects, or an object factory.

Having a class defined, you can create as many objects as needed. These objects are called instances of this class. All the instances created with a given class will have the same structure and behaviour. They will only differ regarding their state, i.e regarding the value of their attributes. Namely, names and sequences will not be the same for all sequences objects that will be created.

Instance

Instances are objects created with a class.

The behaviour of the instances, i.e, the operations that are available for them either to modify them or to ask them for services, is described by functions associated to the class. These functions are called methods.

Method

A method is a function defined for a class, specifying the behaviour of the class instances.

Important

Classes and instances are not the same: the class is the maker, whereas the instance is the object being made, according to the model defined by the class.

18.2.4. Creating objects

How are we going to create DNA objects? You have actually already created objects throughout this course: strings, lists, etc... However, most of the time the objects you have manipulated were not directly created by your own code, but rather by other components: for instance the “[]” operator creates a list, the “""” operator creates a string, the open function creates a file handle, etc...

But the actual direct syntax to create objects or to instantiate a class, i.e to create an instance of class, is by calling a function with the same name as the class. So, in order to create an instance of the DNA class we have just defined, i.e an object which handles a DNA sequence, you do:

>>> s1 = DNA()
	
After the DNA() function is called, the instance of the class DNA is referred to by the s1 variable, just like with any Python value. You could also provide more information at instantiation. Let's say that we can create the name and seq attributes, we can do :
>>> s2 = DNA('seq2', 'acaagatgccattgtcccccggcctcctgctgctgctgctctccggggcca')
	
This time, the new DNA instance has been created with values for name and seq attributes. You can also use keyword arguments (especially if you are no longer sure in what order they should be entered) :
>>> s2 = DNA(name='seq2', seq='acaagatgccattgtcccccggcctcctgctgctgctgctctccggggcca')

So, this creates the object and makes it available from the s2 variable. You can now use this object to call DNA class methods, by using the dot operator:

>>> s2.gc()
0.66
 	
The interpretor can find which class s2 belongs to, and calls the gc method that is defined for the DNA class. You can also actually access to object's attributes by using the dot operator:
>>> s2.name
'seq2'
>>> s2.seq
'acaagatgccattgtcccccggcctcctgctgctgctgctctccggggcca'
 	
The setname method enables to change the name attribute of our object:
>>> s2.setname('new name')
>>> s2.name
'new name'