Stage 3: Object-Oriented Programming

Chapter 1: Basic OOP and Using Classes

When Functions fail to Suffice

The Golden Rule of Programming is arguably 'keep it readable and reduce repetition.' Simplicity in code makes it more easily understandable to outside programmers and often increases performance. Oftentimes, using functions to store large amounts of data as arguments can be a convoluted and confusing process that increases needless repetition.

Object Oriented Programming

Thankfully, there exists an alternative to the use of functions. This is called Object Oriented Programming (OOP). OOP allows coders to use 'templates' or 'blueprints' of sorts called classes to organize the defining characteristics of desired manifestations of that class, called objects or instances of that class. Objects themselves have procedures of their own, known as methods. Thinking more abstractly in this manner presents a more logical, more freely-flowing paradigm that enables coders to work from a set of predetermined variables rather than copying and pasting content from simple functions.

Python Standard Library

The Python language comes complete with a so-called Standard Library, which allows coders to import desired classes and their specific class and instance variables for use within the program. Though the PSL contains many useful classes and associated functions, it is not comprehensive, and coders may download and install additional classes of their own choosing or their own design.

Chapter 2: Basic OOP and Making Classes

Defining __init__

Though the Python Standard Library is quite extensive, coders may also feel the need to create their own situation-specific classes as well. This can be done through the naming of the class, followed by the defining of the function __init__, a built-in Python function that initializes space for a new instance of the class. The first argument is 'self,' and the following arguments are desired attributes for instances of the class.

Inheritance

Like CSS, the concept of inheritance also applies to Python and Object-Oriented Programming. In classes, attributes of a parent class may be recycled and reused in a child class for further use in the program. This concept is one of the advantages of OOP, as it simplifies what would otherwise be an extensive list of copy/pasted functions and attributes.

Instance vs. Class Variables

Classes may have instance variables and class variables, which serve as attributes of the class in question. Instance variables are found defined within the __init__ function of the class, and are associated with every instance or object of the class. Class variables are defined and used class-wide, regardless of the object-specific instance variables used. They are the same for all instances of the class, and often remain constant throughout the entire program, meaning that they are not given new values every few lines of code.