>A Byte of Python

Chapter 11. Object-Oriented Programming

In our programs till now, we designed our program around functions or blocks of statements which manipulate data. This is called the procedural programming paradigm. There is another way of organising your program which is to combine data and functionality and wrap it inside what is called an object. This is called the object oriented programming paradigm. Most of the time you can use procedural programming but when you want to write large programs or if you have a program that is better suited to it, you can use object oriented programming techniques.

Classes and objects are the two main aspects of object oriented programming. A class creates a new type where objects are instances of the class. An analogy is that you can have variables of type int which translates to saying that variables that store integers are variables which are instances (objects) of the int class.

Objects can store data using ordinary variables that belong to the object. Variables that belong to an object or class are called as fields. Objects can also have functionality by using functions that belong to the class. Such functions are called methods. This terminology is important because it helps us to differentiate between a function which is separate by itself and a method which belongs to an object.

Remember, that fields are of two types - they can belong to each instance (object) of the class or they belong to the class itself. They are called instance variables and class variables respectively.

A class is created using the class keyword. The fields and methods of the class are listed in an indented block.