Object Oriented Programming

OOP vs Procedural programming
Both are high level programming used to find solutions to problems

Procedural programming uses a top-down, systematic approach to break down tasks into various functions which work together to solve the overall problem. It makes use of sequencing, selection and iteration.

in OOP the use of instantiation (i.e objects being instances of classes) allows for variables or data to be set as public or private. this protects the integrity and security of data.

However, this additional layer of security does not replace…….

Classes are examples of encapsulation. It is when data and methods that act upon those data are combined into one (getters and setters within the class)

Encapsulation allows for data integrity and security by being able to set variables as “private”.
This ensures that specific private data cannot be accessed outside of the class and can prevent accidental change and loss of that data.

Classes can be reused.

The properties and methods of the original class can be inherited by the new copy of the class (Child class)

The new (copied) class can have their own individual properties and methods while retaining those that have been inherited from the parent class.

Polymorphism
This is the ability to process objects differently depending on their class or data type.

It can allow for a method within the child class to override a method that has been set within a parent class.

the same method name can still be used within the child class but what the method actually does can be different

Creating Class Blueprints

Object Oriented Programming (OOP) in python can be incredibly useful. The main idea is that an “object” is an entity with multiple variables and functions related to it. “Objects” are created from “blueprints” these are classes which define how an object should be created.

In this instance the class is defining how to “create” a Room, a Room has 4 variable linked to it with functions to return or set the values of these variables.
When these “setters” are called and passed a value this then replaces the current value of a specific variable. class functions however can do a multitude of things, for example. The function “get_details” in the image above has both a loop and print function.

Generating Classes

To generate a class from a “blueprint” in a different .py file, the “blueprint” must first be imported. Both the .py file from where it is being imported and the name of the class are case sensitive. Also if the .py file is not in the same location as the one you are importing to you will need to specify the location.

Classes can be generated manually as done here and assigned to a variable there for being given a “name” or they can be generated through loops or other methods.

Create your own website, free on WordPress.com.Create your own website.
Start free