GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing pynative.com — Python Object-Oriented Programming (OOP) Archives Python Object-Oriented Programming (OOP)In this series, you will learn OOP (Object Oriented Programming) in Python. OOP concepts include object, classes, constructor and encapsulation, polymorphism, and inheritance. Python Object-Oriented Programming This Python OOP series contains the following in-depth tutorial. You can directly read those.
What is Object Oriented Programming in PythonObject-oriented programming (OOP) is a programming paradigm based on the concept of "objects". The object contains both data and code: Data in the form of properties (often known as attributes), and code, in the form of methods (actions object can perform). An object-oriented paradigm is to design the program using classes and objects. Python programming language supports different programming approaches like functional programming, modular programming. One of the popular approaches is object-oriented programming (OOP) to solve a programming problem is by creating objects ![]() An object has the following two characteristics:
For example, A Car is an object, as it has the following properties:
One important aspect of OOP in Python is to create reusable code using the concept of inheritance. This concept is also known as DRY (Don't Repeat Yourself). Class and ObjectsIn Python, everything is an object. A class is a blueprint for the object. To create an object we require a model or plan or blueprint which is nothing but class. For example, you are creating a vehicle according to the Vehicle blueprint (template). The plan contains all dimensions and structure. Based on these descriptions, we can construct a car, truck, bus, or any vehicle. Here, a car, truck, bus are objects of Vehicle class A class contains the properties (attribute) and action (behavior) of the object. Properties represent variables, and the methods represent actions. Hence class includes both variables and methods. ![]() Object is an instance of a class. The physical existence of a class is nothing but an object. In other words, the object is an entity that has a state and behavior. It may be any real-world object like the mouse, keyboard, laptop, etc. Read More: Classes and Objects in Python Class Attributes and MethodsWhen we design a class, we use instance variables and class variables. In Class, attributes can be defined into two parts:
Inside a Class, we can define the following three types of methods.
Read Python Class Method vs. Static Method vs. Instance Method to understand the difference between all three class methods. Creating Class and ObjectsIn Python, Use the keyword The docstring is not mandatory but recommended to use. We can get docstring using Syntax
We can create any number of objects of a class. use the following syntax to create an object of a class.
OOP Example: Creating Class and Object in Python
Output: Employee: Harry 12000 ABC Company Employee: Emma 10000 ABC Company
![]() Constructors in PythonIn Python, a constructor is a special type of method used to initialize the object of a Class. The constructor will be executed automatically when the object is created. If we create three objects, the constructor is called three times and initialize each object. The main purpose of the constructor is to declare and initialize instance variables. It can take at least one argument that is A constructor is optional, and if we do not provide any constructor, then Python provides the default constructor. Every class in Python has a constructor, but it's not required to define it. Read More: Encapsulation in PythonIn Python, encapsulation is a method of wrapping data and functions into a single entity. For example, A class encapsulates all the data ( methods and variables). Encapsulation means the internal representation of an object is generally hidden from outside of the object's definition. ![]() Need of Encapsulation Encapsulation acts as a protective layer. We can restrict access to methods and variables from outside, and It can prevent the data from being modified by accidental or unauthorized modification. Encapsulation provides security by hiding the data from the outside world. Read the complete guide on Encapsulation in Python. Example: Encapsulation in PythonWhen you create a class, it means you are implementing encapsulation. A class is an example of encapsulation as it binds all the data members (instance variables) and methods into a single unit. In Python, we do not have access modifiers, such as public, private, and protected. But we can achieve encapsulation by using prefix single underscore and double underscore to control access of variable and method within the Python program.
Output: Name is Jessa and salary is 40000 AttributeError: 'Employee' object has no attribute '__salary' In the above example, we create a class called Polymorphism in PythonPolymorphism in OOP is the ability of an object to take many forms. In simple words, polymorphism allows us to perform the same action in many different ways. Polymorphism is taken from the Greek words Poly (many) and morphism (forms). Polymorphism defines the ability to take different forms. For example, The student can act as a student in college, act as a player on the ground, and as a daughter/brother in the home. Another example in the programming language, the + operator, acts as a concatenation and arithmetic addition. ![]() Read the complete guide on Polymorphism in Python. Example: Using Polymorphism in PythonFor example, In the below example, calculate_area() instance method created in both Circle and Rectangle class. Thus, we can create a function that takes any object and calls the object's calculate_area() method to implement polymorphism. Using this object can perform Polymorphism with class methods is useful when we want objects to perform the same action in different ways. In the below example, both objects calculate the area (same action) but in a different way (different formulas)
Output: Area of circle : 78.5 Area of Rectangle : 50 Inheritance In PythonIn an Object-oriented programming language, inheritance is an important aspect. In Python, inheritance is the process of inheriting the properties of the parent class into a child class. The primary purpose of inheritance is the reusability of code. Using inheritance, we can use the existing class to create a new class instead of recreating it from scratch. Syntax
Read the complete guide on Inheritance in Python Example: Use of Inheritance in Pythonin the below example, From a vehicle class, we are creating a Car class. We don't need to define common attributes and methods again in Car class. We only need to add those attributes and methods which are specific to the Car. In inheritance, the child class acquires all the data members, properties, and functions of the parent class. Also, a child class can customize any of the parent class methods.
Output: BMW X1 Black 35000 BMW X1 change gear to number 5 Next StepsAll Python OOP tutorials: |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |