Ticker

6/recent/ticker-posts

Python: Object Oriented Programming (OOPs)

What is Class: 

=> In Python, everything is an object. To create objects we required some Model or Plan or Blueprint, which is nothing but class.

=> We can write a class to represent the properties (attributes) and actions (behavior) of objects.

=> Properties can be represented by variables

=> Actions can be represented by Methods.

=> Hence class contains both variables and methods.

How to Define a class?

We can define a class by using the class keyword. 

Syntax:

class className:
 ''' documenttation string '''
 variables:instance variables,static and local variables
 methods: instance methods,static methods,class methods

The documentation string represents a description of the class. Within the class, doc string is always optional. We can get doc string by using the following 2 ways.

1. print(classname.__doc__)
2. help(classname)

Example:

1) class Student: 
2) ''''' This is student class with required data''' 
3) print(Student.__doc__) 
4) help(Student) 

Within the Python class we can represent data by using variables. 

There are 3 types of variables are allowed. 

  1. Instance Variables (Object Level Variables)
  2. Static Variables (Class Level Variables)
  3. Local variables (Method Level Variables)

Within the Python class, we can represent operations by using methods. The following are various types of allowed methods 

  1. Instance Methods
  2. Class Methods 
  3. Static Methods

Example for class:

1) class Student: 
2) '''''Developed by durga for python demo''' 
3) def __init__(self): 
4) self.name='durga' 
5) self.age=40 
6) self.marks=80 
7) 
8) def talk(self): 
9) print("Hello I am :",self.name) 
10) print("My Age is:",self.age) 
11) print("My Marks are:",self.marks) 

What is Object:

Pysical existence of a class is nothing but object. We can create any number of objects for a class.

Syntax to create object: referencevariable = classname()

Example: s = Student()

What is Reference Variable:

The variable which can be used to refer object is called reference variable.

By using reference variable, we can access properties and methods of object.

Program: Write a Python program to create a Student class and Creates an object to it. Call the method talk() to display student details

1) class Student: 
2) 
3) def __init__(self,name,rollno,marks): 
4) self.name=name 
5) self.rollno=rollno 
6) self.marks=marks 
7) 
8) def talk(self): 
9) print("Hello My Name is:",self.name) 
10) print("My Rollno is:",self.rollno) 
11) print("My Marks are:",self.marks) 
12) 
13) s1=Student("Durga",101,80) 
14) s1.talk() 
Output:
D:\durgaclasses>py test.py
Hello My Name is: Durga
My Rollno is: 101
My Marks are: 80

 Self variable:

self is the default variable which is always pointing to current object (like this keyword in Java)

By using self we can access instance variables and instance methods of object.

Constructor Concept:

=> Constructor is a special method in python.

=> The name of the constructor should be __init__(self)

=> Constructor will be executed automatically at the time of object creation.

=> The main purpose of constructor is to declare and initialize instance variables.

=> Per object constructor will be exeucted only once.

=> Constructor can take atleast one argument(atleast self)

=> Constructor is optional and if we are not providing any constructor then python will provide default constructor.

Program to demonistrate constructor will execute only once per object:

1) class Test: 
2) 
3) def __init__(self): 
4) print("Constructor exeuction...") 
5)
6) def m1(self): 
7) print("Method execution...") 
8) 
9) t1=Test() 
10) t2=Test() 
11) t3=Test() 
12) t1.m1() 
Output
Constructor exeuction...
Constructor exeuction...
Constructor exeuction...
Method execution...

Differences between Methods and Constructors:

Method 
Constructor 
 Name of method can be any name  Constructor name should be always __init__ 
 Method will be executed if we call that method  Constructor will be executed automatically at the time of object creation. 
 Per object, method can be called any number of times.  Per object, Constructor will be executed only once 
 Inside method we can write business logic  Inside Constructor we have to declare and initialize instance variables 

Types of Variables:

Inside Python class 3 types of variables are allowed.

  1. Instance Variables (Object Level Variables)
  2. Static Variables (Class Level Variables)
  3. Local variables (Method Level Variables)

1. Instance Variables:

If the value of a variable is varied from object to object, then such type of variables are called 

instance variables.

For every object a separate copy of instance variables will be created.

Where we can declare Instance variables:

  1. Inside Constructor by using self variable
  2. Inside Instance Method by using self variable
  3. Outside of the class by using object reference variable

1. Inside Constructor by using self variable:

We can declare instance variables inside a constructor by using self keyword. Once we creates object, automatically these variables will be added to the object

Example:

1) class Employee: 
2) 
3) def __init__(self): 
4) self.eno=100 
5) self.ename='Durga' 
6) self.esal=10000 
7) 
8) e=Employee()
9) print(e.__dict__) 
Output: {'eno': 100, 'ename': 'Durga', 'esal': 10000}

2. Inside Instance Method by using self variable:

We can also declare instance variables inside instance method by using self variable. If any instance variable declared inside instance method, that instance variable will be added once we call taht method.

 Example:

1) class Test: 
2) 
3) def __init__(self): 
4) self.a=10 
5) self.b=20 
6) 
7) def m1(self): 
8) self.c=30 
9) 
10) t=Test() 
11) t.m1() 
12) print(t.__dict__) 
Output
{'a': 10, 'b': 20, 'c': 30}

3. Outside of the class by using object reference variable:

We can also add instance variables outside of a class to a particular object.

1) class Test: 
2) 
3) def __init__(self): 
4) self.a=10 
5) self.b=20 
6) 
7) def m1(self): 
8) self.c=30 
9) 
10) t=Test() 
11) t.m1() 
12) t.d=40 
13) print(t.__dict__) 
Output {'a': 10, 'b': 20, 'c': 30, 'd': 40}

How to access Instance variables:

We can access instance variables with in the class by using self variable and outside of the class by using object reference.

1) class Test: 
2) 
3) def __init__(self): 
4) self.a=10 
5) self.b=20 
6) 
7) def display(self): 
8) print(self.a) 
9) print(self.b) 
10) 
11) t=Test() 
12) t.display() 
13) print(t.a,t.b) 
Output
10
20
10 20

How to delete instance variable from the object:

1. Within a class we can delete instance variable as follows

 del self.variableName

2. From outside of class we can delete instance variables as follows

 del objectreference.variableName

Example:

1) class Test: 
2) def __init__(self): 
3) self.a=10 
4) self.b=20 
5) self.c=30 
6) self.d=40 
7) def m1(self): 
8) del self.d 
9) 
10) t=Test() 
11) print(t.__dict__) 
12) t.m1() 
13) print(t.__dict__) 
14) del t.c 
15) print(t.__dict__) 
Output
{'a': 10, 'b': 20, 'c': 30, 'd': 40}
{'a': 10, 'b': 20, 'c': 30}
{'a': 10, 'b': 20}

1. Static variables:

If the value of a variable is not varied from object to object, such type of variables we have to declare with in the class directly but outside of methods. Such type of variables are called Static variables.

For total class only one copy of static variable will be created and shared by all objects of that class.

We can access static variables either by class name or by object reference. But recommended to use class name.

Instance Variable vs Static Variable:

In the case of instance variables for every object a seperate copy will be created,but in the case of static variables for total class only one copy will be created and shared by every object of that class.

Various places to declare static variables:

  1. In general we can declare within the class directly but from out side of any method
  2. Inside constructor by using class name
  3. Inside instance method by using class name
  4. Inside classmethod by using either class name or cls variable
  5. Inside static method by using class name
1) class Test: 
2) a=10 
3) def __init__(self): 
4) Test.b=20 
5) def m1(self): 
6) Test.c=30 
7) @classmethod 
8) def m2(cls): 
9) cls.d1=40 
10) Test.d2=400 
11) @staticmethod 
12) def m3(): 
13) Test.e=50 
14) print(Test.__dict__) 
15) t=Test() 
16) print(Test.__dict__) 
17) t.m1() 
18) print(Test.__dict__) 
19) Test.m2() 
20) print(Test.__dict__) 
21) Test.m3() 
22) print(Test.__dict__) 
23) Test.f=60 
24) print(Test.__dict__) 

How to access static variables:

  1. inside constructor: by using either self or classname
  2. inside instance method: by using either self or classname
  3. inside class method: by using either cls variable or classname
  4. inside static method: by using classname
  5. From outside of class: by using either object reference or classnmae
1) class Test: 
2) a=10 
3) def __init__(self): 
4) print(self.a) 
5) print(Test.a) 
6) def m1(self): 
7) print(self.a) 
8) print(Test.a) 
9) @classmethod 
10) def m2(cls): 
11) print(cls.a) 
12) print(Test.a) 
13) @staticmethod 
14) def m3(): 
15) print(Test.a) 
16) t=Test() 
17) print(Test.a) 
18) print(t.a) 
19) t.m1() 
20) t.m2() 
21) t.m3() 

Where we can modify the value of static variable:

Anywhere either with in the class or outside of class we can modify by using classname.

But inside class method, by using cls variable.

Example:

1) class Test: 
2) a=777 
3) @classmethod 
4) def m1(cls): 
5) cls.a=888 
6) @staticmethod 
7) def m2(): 
8) Test.a=999 
9) print(Test.a) 
10) Test.m1() 
11) print(Test.a) 
12) Test.m2() 
13) print(Test.a) 
Output
777
888
999

Local variables:

Sometimes to meet temporary requirements of programmer,we can declare variables inside a method directly,such type of variables are called local variable or temporary variables.

Local variables will be created at the time of method execution and destroyed once method completes.

Local variables of a method cannot be accessed from outside of method.

Example:

1) class Test: 
2) def m1(self): 
3) a=1000 
4) print(a) 
5) def m2(self): 
6) b=2000 
7) print(b) 
8) t=Test() 
9) t.m1() 
10) t.m2() 
Output
1000
2000


 "Python Object Oriented Programming Questions”

"Python Object Oriented Programming Concepts"

"Python Object Oriented Programming Practice Problems"

"Object Oriented Programming Oop In Python"

"Python Object Oriented Programming Examples"

"Python Object Oriented Programming Exercises"

"Class And Object In Python"

"What Is Object In Python"

"Python 3 Object Oriented Programming"