In the functions and procedures section of learning Python we looked at procedural programming. When you create a program using functions and procedures, your program soon gets big and you have to pass and receive values from one function to the other, your code soon becomes a matrix that makes it difficult for you to keep track of the flow of your program. This is often called spaghetti code, where and values are being passed forward, backward and there are lots of interdependencies all over your code, if you change one thing then you need to remember to change it somewhere else in your code - nightmare!. However Object Oriented Programming helps reduce this problem.
WHAT IS OBJECT ORIENTED PROGRAMMING
Object Oriented Programming provides a way to decompose your program and create a blue print for objects/actions/sections needed. These blue prints are called a class, within the class you have functions, functions within a class are referred to as methods. Variables within the class with a self. prefix are known as attributes. By using the class name methods and attributes can be used throughout your program without the issues you would otherwise face with local and global variables and the passing of parameters.
THE FOUR 4 PILLARS OF OOP
ENCAPSULATION Grouping related variables and items together to help simplify your program by encapsulating them in one place, in turn this makes it easy to re-use/call variables. ABSTRACTION Is a process of filtering out only the details needed, reducing complexity and interdependencies within your code. Hiding or removing unnecessary information. INHERITTANCE Is the process of allowing multiple sections of code to inherit/use attributes from another piece of code. Rather that duplication or code, if one section of your code needs the same values as another is already using the process of inheritance provides an efficient method of doing this. For example if each page in your GUI project has the same styling, then you can inherent the styles from a style sheet rather than re-coding the styles each time you create a new page. POLYMORPHISM Polymorphism means 'Many Forms' Poly(Many) Morphism(Forms). It is a method that helps to reduce the need for long if statements, this is done by enabling rendering/relationships depending on the type of object you are referencing to rather than the attributes of the object.
SECTION 1 | ATTRIBUTES AND METHODS
In OOP, a class is like a blueprint, and an object is an instance of that blueprint. The class can have attributes (characteristics or properties) and methods (functions or behaviours).
Attributes: Attributes are variables that store data for an instance or for the class itself.
Instance Variables: These are variables that are unique to each instance of the class. They are defined within methods and are accessed using the self keyword.