Python – Object Oriented Concepts

Python is a high-level, interpreted programming language that supports object-oriented programming (OOP) concepts. Object-oriented programming is a programming paradigm that uses objects and their interactions to design applications. In Python, everything is an object, including integers, strings, lists, functions, and even classes.

Some of the fundamental object-oriented concepts in Python include:

  1. Classes and Objects: A class is a blueprint for creating objects that defines the attributes and methods of an object. An object is an instance of a class.
  2. Encapsulation: This is the process of hiding data and behavior within an object, preventing external code from accessing and modifying it.
  3. Inheritance: This is a mechanism for creating new classes that are based on existing classes. The new class inherits the attributes and methods of the existing class.
  4. Polymorphism: This is the ability of an object to take on many forms. In Python, this is often achieved through method overriding and overloading.
  5. Abstraction: This is the process of simplifying complex systems by breaking them down into smaller, more manageable components. In OOP, this is often achieved by defining abstract classes and methods.

In Python, you can create a class using the class keyword. Here is an example:

class Person:
    def __init__(self, name, age): = name
        self.age = age
    def say_hello(self):
        print(f"Hello, my name is {} and I am {self.age} years old.")

This creates a Person class that has a name and age attribute, as well as a say_hello method. You can create an object of this class like this:

person = Person("John", 30)

This will output:

Hello, my name is John

Using OOP concept in Python program:

  • Data becomes active.
  • Code is reusable, thus less code may have to be written.
  • Ability to simulate real world events much more effectively
  • programmer is able to produce faster, more accurate and better written applications.

How Python OOP is Different From other OOPs?

Like Python, you can not write a single piece of code in Java without using class. You can do so in Python only, even many projects don’t use it.

Python is an interpreted language. It means it execute the codes line-by-line, if your programs has some error, that error will be shown when you line of code will get executed, that is not the case with JAVA.

Relationship between Classes and Objects

  • A class is a template for objects. It contains the code for all the object’s methods.
  • A class describes the abstract characteristics of a real-life things.
  • An instance is an object of a class created at run-time.
  • There can be multiple instances of a class.

Creating a Class

To create a class, you have to use ‘class’ keyword followed by the name of the class and parenthesis.

A class during creating can accepts arguments inside it. And the statements are written in the body of class parenthesis. Properties and methods are defined inside the class body.

Let’s look at the below example.

#Creating a class
class number():

#Creating Instance of class
x = number()

<__main__.number object at 0x01951670>

In the above example, a class is created of name number and then the statements wrote inside the class. To create an instance of a class, what you need to do is that you need to take a variable and call the name of the class and follow the pattern which will create the class for you or create the instance of a class for you. Here we have print(x) to show the object’s memory location.

<main.number object at 0x01951670>

Definition of Method

Method is defined within a class. The first parameter in the definition of a method has to be a reference “self” to the instance of the class.

class Sharequery():
    def Greeting(self):
        print("Happy Learning!")

obj = Sharequery()

Happy Learning!

Here in the above example, “self” points to the class object. “obj” is the object of the class. Instead of self.Greeting(), we write obj.Greeting()

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial