Object Oriented Programming (or OOPS for short) is a programming style that improves readability, reusability, and allows for easier collaboration of code between developers and builds a robust codebase that can be retrieved and modified with ease.
It's like a map that gives direction and structure to your program. This structure acts as the core for your codebase and gives an idea of the child functions and variables.
Introduction to Object-Oriented Programming
Object-oriented programming is essential in software development, as it defines a structure for your program. As the name suggests, objects play an important role in object-oriented programming. An object can be further defined as an instance of a class.
Class
Classes are a design or a structure in which the program follows a certain structure. An object or any other superclass inherits from the given class. A class is said to be one of the primary components of an OOP structure. In the above example, we can see that Pi_value is a class from which Pi_obj inherits the structure and the value “self.val”
Object
An object is an instance that inherits its structure from a class structure. In simple terms, an object is an empty instance when assigned a certain class inherits the complete structure including any data, variables, and functions in the given class. Here’s an example of this:
class Pi_value:
def __init__(self):
self.val = 3.1415926535
# Now we assign an object to the above class
Pi_obj = Pi_value()
print(Pi_obj.val)
#This should print the value of pi stored in self.val
Object-Oriented Design Goals
Object-oriented programming is built around the key principles: robustness, adaptability, and reusability.
Robustness
A program would need to not only return the expected output but also be able to handle unexpected inputs and be able to manage exceptions without crashing. Exception handling is a core feature of a robust software program.
Adaptability
Adaptability is the ability of the software to adapt to the changing system environments i.e. it should be able to handle any changes in the specifications of the development or production environments and adapt accordingly. On a similar note is Portability.
Portability is the ability of the program to execute on different hardware platforms and development environments with minimal changes.
Reusability
Reusability is similar to adaptability, i.e. your code should be reusable in different applications and software systems without much changes. Having code that can be reused for multiple software systems in your organization can be helpful and prevent coding the same program logic again.
Understanding Inheritance in Python
Inheritance is the among the core principles of OOP. Inheritance helps you understand the relationship between different classes and objects.
Inheritance can be considered as inheriting a data structure from the parent class. If you plan to build a house from a blueprint, your house automatically inherits from the blueprint along with any changes you would like as the construction of your house progresses.. Something similar takes place in OOP inheritance as well, where a new class can be created inheriting from structures from the older one while retaining its own unique structure as well.
In the next section, we will see more about polymorphism and how we can use invoke the same method from different classes in Python.
Polymorphism: Achieving Flexibility in Code
Polymorphism is a method where classes with the same methods can be invoked at once rather than calling them independently. Polymorphism makes handling multiple classes more simple. You can invoke methods with the same name from different classes or get variable data stored in different classes with the same name. This can be used for non-inherited and inherited classes alike.
Encapsulation: Protecting Your Data
Encapsulation is a way of protecting your internal data and methods. However, in Python, there’s no true way of making your data either private or protected as the data and methods from the protected class can be accessed using a child class. It serves more as a message to other developers to be cautious while using a particular class.
In Python, you can declare a protected method using an underscore “_” before the variable. For example, using self._val = 3.1415926535 in the above Pi_value class would make the val variable not accessible to the public.
Abstraction: Simplifying Complex Systems
Abstraction is basically complex data structures, methods, and variables condensed down to the most fundamental parts. An abstraction is the collection of methods, objects and variables, all assembled in a structure.
That structure would only be known to the programmer and not the user. This method of interaction and the process flow between the components, classes, and data variables is known as abstraction. Abstraction simplifies the design methodology by giving clear relationships between the variables, methods, and different classes.
In simple terms, abstraction can be said to be a guide or a map of a path. It gives a general direction on how to proceed to achieve a process or a desired function.
Special Methods and Operator Overloading
In this section, we will be discussing special methods in Python Object-oriented programming and also we will see more about operator overloading.
Among the special methods, we’ll be discussing decorators ( “@”). Decorators are used to reference a method or a class without explicitly calling a function. It can be considered a shorthand for referencing a method or a class. Here’s an example:
class Decorators:
def __init__(self):
self.ret_list = [] #saves all the returns after invoking decorators
pass
def __call__(self, val):
self.ret_list.append(val())
def __repr__(self):
return str(self.ret_list)
decorators = Decorators()
@decorators
def ret_func():
return "Hello World"
@decorators
def pizza_func():
return "My favorite pizza is pepperoni"
print(decorators)
Now you can invoke the decorators class using “@decorators” before your function.
Operator Overloading
Operator overloading is the method where you can overload the function of an operator and assign more methods to it. An example of operator overloading is the plus operator.
“+” operator can be used for simple addition and it can be used for string concatenation too. The below image shows the dual function of the “+” operator.
This is an example of operator overloading, where the operator can handle multiple inputs and data types.
Real-World Applications of OOP in Python
OOP can be used for various real-world applications such as in software development, where code would need to be reused and many different programmers work on the same project at the same time. It provides structure to your software and can make your code more understandable as well as reusable if you want to use the same codebase in the future. OOP is a powerful concept that can help in various fields in software design and architecting.
Here are some real-world applications that use OOP as their core:
Java: The Java programming language is built with OOP as its main goal. Even simple tasks such as printing and getting user input require you to initialize the Main class before declaring the method.
Video game development: Video games can include a massive codebase and structuring the methods and variables with various different classes can help reduce/optimize the codebase and promote collaboration among developers and make the codebase more understandable/easier to process.
These are a few of the many applications of OOP in Python. Pygame, Tkinter, and many other similar libraries in Python use OOPS extensively.
Conclusion and Further Reading
OOP in Python is a powerful tool that can help with structuring your data and methods as an object that can be recalled as required. Object inheritance, encapsulation, polymorphism, and method overloading are some of the methods of OOP. OOP is a crucial concept in software development and reusability. Its principles can help optimize and make complex code more understandable. OOP is a core concept in software architecting, and optimization and has many different real-world applications.
Comments
Post a Comment