= (1.0, 1.0, 0.0)
d.complement print (d.complement)
Classes and objects
Python is an object oriented language. We briefly mentioned that objects are self-contained entities that bundle together data and functionality. We also discussed how we can have multiple copies of objects.
Pretty much everything in python is an object. It may not be obvious at this point why it is useful to have a language that works this way and, in fact, this is not something we will dwell upon because you will come across it from many different angles as we proceed and you’ll become familiar with the concepts that way.
However, it is important to be able to recognise that certain things are objects and that objects are defined by a language construct called a class
if only to be able to read the documentation and know what to expect.
Let’s just look at a couple of examples - ways to bundle up some data easily:
The colour
class is defined with two attributes: rgb
and description
. When we create instances of the colour
class, we can access these attributes using dot notation:
colour
can be thought of as a data container that standardises how we refer to colours. By default it is “Black” but we can always change the values. In the example above, we changed the rgb
value but we did not update the description. Remember this for later !
Another thing, we can also do this:
But, does this do anything to the class itself ? In other words, do you think the following will work or will it fail ? If you are not sure, try it out in exercise 2.
print (c.complement)
When we make an instance of a class (c
or d
are instances of the colour
class), it is a new object in memory that can be modified quite extensively and this means it can fall out of sync with the class definition if we are not careful. Again, remember this for later !
The colour
class defines how objects will look once defined, but each ‘instance’ of the class (here c
and d
) lives a life of its own once built. This is even true if we re-define the class:
Even though we re-define the class, the instances that have already been defined are not affected. Now here is something interesting:
The class definition is held on the object and, as a result, the objects that are defined from classes that have the same name do not have to have the same type. You can see that the c
object is still a colour
object, but so is e
!
Classes with Methods
The point of classes is to collect up data and the operations that we want to perform on that data and pass them around the program as a single object. We didn’t really manage to do that yet, but we are nearly there.
The rgb
value of the colour has a complement that can be calculated automatically, so we should just write a function to do that and bind it to the data. This eliminates the need to manually synchronise the data each time we assign a new colour, and that is a way to reduce the likelihood of making a mistake.
We define the new version of the class like this:
Here we have introduced the defaults as arguments that we can supply when we make an instance of the class. We also switched complement
to be a function (a method of the class) which slightly changes how it is used.
self
has a special meaning - it refers to the object “itself” and it is available within the object itself to set to the internal data or call the methods. This way to define the class also makes the help
function more useful.
Here is a slightly more elaborate version of the class. It has additional methods attached and each one of them has a docstring that describes in a line-or-two what the method does.
Operator Overloading
One of the extensibility secrets of python is that all the language operators are defined by special ‘invisible’ functions on the classes. That means we can define +
, -
, *
, /
etc for objects. Think of the way multiplication is defined differently for numbers versus matrices, for example. Here is how we might use operator overloading to refer to the different colour mixing methods:
That’s all we really need to know about classes to get started using them, but if you want to know more about that last example, you’ll need to know about this page at the very least: Object Model: numeric types from the python documentation. This is mostly advanced stuff, but if you find yourself being given a class description when you ask for help on something, this information will help you.