Introduction to Object-Oriented Programming
This module addresses the advanced Object Oriented Programming (OOP) issues that are at the heart of Python programming.
The object-oriented approach is an evolution of good design practices that go back to the very beginning of computer programming.
This very important approach is present in most computer applications because it allows programmers to model entities representing real-life objects. Moreover, OOP allows programmers to model interactions between objects in order to solve real-life problems in an efficient, comfortable, extendable, and well-structured manner.
Imagine that the text you are now reading was rendered in a web browser created with OOP, and even the mouse cursor image is displayed using an application created with OOP. The same applies to spreadsheet handling (treat a spreadsheet as a collection of objects), and many, many other applications.
This chapter assumes that you are familiar with the basics of OOP, so to establish an understanding of common terms, we should agree on the following definitions:
- class — an idea, blueprint, or recipe for an instance;
- instance — an instantiation of the class; very often used interchangeably with the term 'object';
- object — Python's representation of data and methods; objects could be aggregates of instances;
- attribute — any object or class trait; could be a variable or method;
- method — a function built into a class that is executed on behalf of the class or object; some say that it’s a 'callable attribute';
- type — refers to the class that was used to instantiate the object.
Now that we’re starting to discuss more advanced OOP topics, it’s important to remember that in Python everything is an object (functions, modules, lists, etc.). In the very last section of this module, you'll see that even classes are instances.
Why is everything in Python organized as objects?
Because an object is a very useful culmination of all the terms described above:
- it is an independent instance of class, and it contains and aggregates some specific and valuable data in attributes relevant to individual objects;
- it owns and shares methods used to perform actions.
The following issues will be addressed during this and the next module:
- creation and use of decorators;
- implementation of core syntax;
- class and static methods;
- abstract methods;
- comparison of inheritance and composition;
- attribute encapsulation;
- exception chaining;
- object persistence;
- metaprogramming.