Python Programming
About Lesson

Introduction to OOP
Object Oriented Programming is one of the different programming paradigms like procedure oriented, function oriented etc. In this programming approach the program logic is think in terms of objects. Every object has some attributes and abilities. Objects can work and communicate with the world and even with each with the help of methods. Object can be representative of any real-world entity. For example, car is an object, Table is an object, man is an object and so on. A group of objects can have common behaviour and similar attributes. So we can create a model or blueprint that represent those objects. Like Alto, Dezire, Baleno are car objects and ‘four-wheeler’ or ‘Maruti-car’ can be considered as a blue print or model. In Object oriented programming, the blue print or model which is used to create number of objects is known as class.

Class and Objects
In another way, class a can be considered as a user-defined data type. Consider integer data type of C language, we use int data type to declare variables. What is a variable? It is a data element that is used to hold some kind of data. But before that, the system must know how to allocate memory for that data element. The attributes or characteristics of variables are defined by its data type. We can say data type specifies the characteristics and operations possible on each data element that is declared out of that data type. Similar to that, class can be defined as a user-defined data type out of variables can be declared. The variables of class are known as objects.

Creating class
In python, a class can be defined using ‘class’ keyword. A class can contains variables and methods. The content variables and methods of a class are known as its members.

Creation of Object or Class instantiation
As we understood object is a variable or instance declared out of the class, each object can hold all the members of its class definition. The memory for the class members will be allocated at the time of instantiation. The object can access its members by using dot(.) operator.

#Example code showing relationship between class and object
class human:
Eyes=2
Nose=1
def walk(Self): //Self is reference parameter
print(“I can walk”)

m1=human() //human() the constructor
print(“I have “,m1.Eyes, “eyes”)
print(“I have “,m1.Nose, “Nose”)
m1.walk()

The ‘self’ parameter
The ‘self’ parameter acts as a reference to the current instance of the class. The name ‘self’ is not mandatory but it must be the first parameter of any function in the class.

#Example code how constructor can call ___init__ method
class mobile:
def __init__(self,Make,Model):
self.Make=Make
self.Model=Model
def show(S):
print(“My Manufacturer :”,S.Make)
print(“My Model :”,S.Model)

m1=mobile(“Vivo”,”X50″)
m1.show()

You cannot copy content of this page