Day 7 : object oriented python
Hello guys,
Previous we learn about python basics which can be call as procedural orientated.
Now we will learn about object oriented python.
There are object oriented languages like C++ and java but python is quite different in syntax but actual concepts are same.
First let's see the concepts in oops
Python OOPs Concepts
It allows us to develop applications using an Object Oriented approach. In Python, we can easily create and use classes and objects.
Major principles of object-oriented programming system are given below.
- Object
- Class
- Method
- Inheritance
- Polymorphism
- Data Abstraction
- Encapsulation
Object
The object is an entity that has state and behavior. It may be any real-world object like the mouse, keyboard, chair, table, pen, etc.
Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code.
Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code.
Class
The class can be defined as a collection of objects. It is a logical entity that has some specific attributes and methods. For example: if you have an employee class then it should contain an attribute and method, i.e. an email id, name, age, salary, etc.
Method
The method is a function that is associated with an object. In Python, a method is not unique to get instances. Any object type can have methods.
Inheritance
Inheritance is the most important aspect of object-oriented programming which simulates the real world concept of inheritance. It specifies that the child object acquires all the properties and behaviors of the parent object.
By using inheritance, we can create a class which uses all the properties and behavior of another class. The new class is known as a derived class or child class, and the one whose properties are acquired is known as a base class or parent class.
provides re-usability of the code.
Polymorphism
Polymorphism contains two words "poly" and "morphs". Poly means many and Morphs means form, shape. By polymorphism, we understand that one task can be performed in different ways. For example You have a class animal, and all animals speak. But they speak differently. Here, the "speak" behavior is polymorphic in the sense and depends on the animal. So, the abstract "animal" concept does not actually "speak", but specific animals (like dogs and cats) have a concrete implementation of the action "speak".
Encapsulation
Encapsulation is also an important aspect of object-oriented programming. It is used to restrict access to methods and variables. In encapsulation, code and data are wrapped together within a single unit from being modified by accident.
Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly synonym because data abstraction is achieved through encapsulation.
Abstraction is used to hide internal details and show only functionalities. Abstracting something means to give names to things so that the name captures the core of what a function or a whole program does.
Abstraction is used to hide internal details and show only functionalities. Abstracting something means to give names to things so that the name captures the core of what a function or a whole program does.
Python Class and Objects
As we have already discussed, a class is a virtual entity and can be seen as a blueprint of an object. The class came into existence when it instantiated.Creating classes in python
In python, a class can be created by using the keyword class followed by the class name. The syntax to create a class is given below.
Syntax
class classname:
Statement
python, we must notice that each class is associated with a documentation string which can be accessed by using <class-name>.__doc__. A class contains a statement suite including fields, constructor, function, etc. definition.
Consider the following example to create a class Employee which contains two fields as Employee id, and name.
Example:
class Employee:
id = 10;
name = "ayush"
def display (self):
print(self.id,self.name)
Here, the self is used as a reference variable which refers to the current class object. It is always the first argument in the function definition. However, using self is optional in the function call.
Instance of the class
class needs to be instantiated if we want to use the class attributes in another class or method. A class can be instantiated by calling the class using the class name.
The syntax to create the instance of the class is given below.
Syntax
<object-name> = <class-name>(<arguments>)
Example
class Employee:
id = 10;
name = "John"
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
emp = Employee()
emp.display()
Output:
The syntax to create the instance of the class is given below.
Syntax
<object-name> = <class-name>(<arguments>)
Example
class Employee:
id = 10;
name = "John"
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
emp = Employee()
emp.display()
Output:
ID: 10 Name: ayush
Contact here if you get any problem
8805271377 WhatsApp
Follow here ❤
@mr._mephisto_ Instagram
There will be no restrictions just feel free to learn.
Share and take one more step to share knowledge to others.
Believe in yourself 🤟 you are awesome.
Be safe, Be happy😁
Take care of yourself and your family
Of course watch movies and series🤟😉
And follow the guidelines of government
Comments
Post a Comment