In this article, MarsDevs presents basic OOP(Object Oriented Programming) concepts in Python.
So, Objects consist of data and methods. Object-oriented programming (OOP) is a programming pattern based on the concept of objects. Object-data and object-methods are properties of objects and functions of objects respectively.
We can relate programming concepts to the real-world entities with the help of OOPs. So, this is the reason why it is popular and useful in programming.
There are also various advantages of OOPs concepts when we use them in programming. All languages are not based on OOPs concepts but C++, and Python use OOPs concepts.
Java is not a pure object-oriented language, and also C language does not support OOPs as it is a Procedural oriented language but not object-oriented.
The most popular way to solve a problem is by creating objects. This concept is known as Object Oriented Programming (OOP). It can easily relate to real-world problems, which are easy to write and understand.
Consider a real-life example, Car which is a real-life object. The pen has various properties and functions or behavior. Color (red/green/blue) and Type (gel/ball) are the properties. Writing and drawing are functions or behaviors of a pen.
Python is a multi-paradigm programming language. Python programming language follows these programming paradigms
In this article, we discuss Object-Oriented Programming
Since OOP consists of objects and they break the program into objects, OOP is suitable for larger problems. It makes code reusable. OOP has a bottom-up approach to solving problems. It breaks big problems into smaller ones, then focuses on solving these small problems first and then integrates them into the solution of the main problem.
Access modifiers are used to limit the accessibility of class variables and methods, outside of the class while implementing the concepts of inheritance. Python has 3 types of access modifiers: public, private, and protected keywords.
Primitive data structures such as numbers, strings, and lists are destined to store simple values in a variable. You can use these primitive data structures to store your name, age, the factorial of a number, or calculations, etc.
You cannot store students' details, as it may contain more than one field and may cause confusion during indexing.
Even if you try to store them in a dictionary, it will be too complicated for the codebase to handle after the expansion. So, to avoid such issues, we use Classes in Python.
A class is used to create a user-defined data structure. Its functions are known as methods that perform a certain operation defined by the developer. The concept of OOP focuses on classes and objects.
Classes also help avoid repeatable code and avoid complex codebases. Classes focus on generalized structures but not on any specific object. For example, Class Student has a name, roll number, and course.
An object is an instance of a class. It is the implementation of the class that actually exists. It has a collection of data and methods. Data is stored in variables, and methods are defined inside the class.
Humans have properties, such as name, age, gender, and city. And the can perform actions such as walk(), talk(), eat(), sleep(), etc. Collectively these properties and actions form a class.
An instance of this Human Class is known as an object.
For a particular Human, say Shyam. So,
name = Shyam,
Age = 28,
Gender = Male,
City = Delhi
Are the properties of Shyam and he Can also walk( ), talk( ), eat( ), sleep( ), so these are the functions/methods of this particular Object. It is the implementation of the Class that actually exists.
Memory allocation happens only when an object or instance is created but memory allocation does not happen only during Class definition.
In Python, a Class can be defined by the keyword class followed by the name of the class and a colon.
Indented code after the colon of the class definition is the class body. In the above class syntax, we have put "pass" just for example code in the indented body of the class. Here you can define your code for the class. "pass" is used to run the code without throwing an error in Python.
The required properties of the Student class are defined in a method called __init__(). Whenever a new object is created, __init__() sets the initial values of the given properties.
It instantiates each new class instance and can take multiple parameters, but the first parameter of __init__() should always be "self". The "self" parameter is the class's current instance.
The "self" parameter indicates the address of the current object of this class, even if you have n objects (or instances) or more in the class. Since it points to the current object of the class, we can also modify the particular object and return the corresponding value. Thus it brings the seperation of concerns.
We can alias in place of "self", but it always needs to be in the first parameter of the class.
These are given three attributes that create and assign an object(student) name, roll number, and course.
Similarly, as __init__ , there are other dunder or magic methods in Python. They have two prefixes and suffix underscores in the method name. These are special types of methods commonly used for operator overloading. Some examples of magic methods are: __init__, __add__, __len__, __repr__, __new__ etc.
The __new__ and __init__ methods are called whenever a class is instantiated. __new__ and __init__ are called when the object is created and the object is initialized respectively. __new__() is a static method that requires passing of a parameter ie cls.
This (cls) means the class that should be instantiated and managed by the compiler.
Python has 2 types of attributes :
These attributes or variables are the same for all the objects of a class. These are defined at the class level and are not set to new values for each new instance created thus they have the same value for all objects of a class.
Consider the following example
Here, the principal is a class attribute that will be the same for all students (or objects) in this school.
These attributes or variables hold are different information for each object of the class. This information depends on the value we pass while creating the instance - different values for all objects of the defined class.
Consider the following example,
Here, there are three instance attributes that create and assign an object’s name, roll number, and course. Each object of the class now can hold different students’ information without affecting each other.
So, now you can understand that we create class attributes hold the same value per instance of a class and an instance attribute hold different values per instance of a class.
We can create an object (or instance) using the class name after the parenthesis. It can be assigned to any variable.
x = ClassName()
Once the objects are instantiated, memory is allocated to them. If you try to compare two different object (using the "==" operator), it will return False, as different memory allocations even though these objects hold the same information.
Consider the following example,
There are two instances of the class Student. We have passed three arguments. There will be an error message saying you have not passed the required arguments because while creating z, no arguments were provided.
__init__() missing 3 required positional arguments: 'name', 'roll_num', and 'course'
We can store their values in the various variables as shown in the above example.
Now, you can see and analyze the output of this example,
Here, we have used the dot (.) operator to access instances and class attributes.
We can also change the value of the class attribute but assigning a new value to it. Consider the following example,
An instance method is a function defined within a class. These functions can only be called from instances of that class ie they are bound to an instance of the class. An Instance also has "self" as the first parameter as we have in the __init__() method of the class.
Consider the following example
Above class Student two instance methods: intro() and read(). These return the introduction of a student and favorite course name respectively. These methods are called only from instances of the given class, so these are known as instance methods or functions.
So, after explaining classes, objects, and methods in Python, we will explain the fundamentals or principles of OOP concepts in Python in Set-2. We will explain the fundamental concepts of OOPs in Python: Inheritance, Encapsulation, Polymorphism, and Data abstraction.