Learn Python Programming

Python – Object Oriented Concepts

Object oriented programming concept is very important thing in Python. Python supports all different programming paradigm. It supports functional programming, procedural oriented programming and object-oriented programming.

Python is an Object-Oriented programming language. It make it easy to create and use classes and objects.

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.

What is Class?

Class is a blueprint used to create objects having same property or attribute as its class.

When you talk about human beings, it has common set of properties: name, gender, occupation, also every human will be doing some kind of common activities: such as speaking, sleeping, walking etc.

The first set of properties that a human beings governs are called Properties in OOP.

The activities that human performs are called methods in OOP.

Thus, a set of properties and methods form a central entity called a class. Here, human is a class which has two central component which is properties and methods.

Class is nothing but an abstraction of some entity which contains common set of properties and methods.

If you look at a bicycle or a two wheeler. A two wheeler basically the blueprint of having two wheels and a brake. That is the basic blueprint. And from that blueprint, it can have various objects, whether it is automated vehicle, or it is not automated vehicle. It might be of red color or blue color or black color. It can keep changing but doesn’t change the fact that this is still a particular implementation of a particular instance of the cycle object.

Like human being for example, an anatomy of a human being is the same across all the seven billion people in the world. It’s the same blueprint of human beings. That a human being will have certain number of muscles and certain number of bones in you body. But still, each and every person is different. So, this is the anatomy of a human being. This is the variation that comes from this anatomy.

What is Object?

An object is an instance of a class which contains variables and methods. The behaviors varies but properties are common. In Python everything is considered as an object.

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()