7. Object Orientated Programming (Optional)#

Note

Object Orientated Programming (OOP) is outside the scope of this module and students will not be assessed on its use. This chapter is included only as an introduction to OOP for those who are curious and can be skipped if required.

Python is an Object Orientated Programming (OOP) which uses objects that contain data and procedures that act upon that object. Use of OOP can help simplify programs and improve their efficiency.

Object orientated programming uses the following:

  • Object - almost everything in Python is an object. We have already met them in the form of NumPy arrays and matplotlib plots.

  • Attribute - an attribute is a data field that is attached to an object.

  • Method - a function that acts on the object.

  • Class - a class is a blueprint for creating objects. It specifies what attributes an object has and the methods that can be applied on the object.

For example, lets say we want to write a program to deal with different types of animals. Each animal has attributes such as the number of legs it has, the genus it belongs to, and what it eats. It may also have methods such as the sound that it makes, its habitat, whether it is nocturnal etc.