Faculty Development Program: Python Programming
JK Lakshmipat University, Jaipur
(3-7, June 2019)
OOPs CONCEPT IN PYTHON
SANTOSH VERMA
Important Note
Please
remember that Python completely
works on indentation.
Usethe tab key to provide indentation to your
code.
Classes and Objects
Classes
Just like every other Object Oriented Programming
language Python supports classes. Let’s look at some
points on Python classes. Classes are created by
keyword class.
Attributes are the variables that belong to class.
Attributes are always public and can be accessed using
dot (.) operator. Eg.: Myclass.Myattribute
# A simple example class
class Test:
# A sample method
def fun(self):
print("Hello")
# Driver code
obj = Test()
obj.fun()
Output: Hello
The self
Class methods must have an extra first parameter in method
definition. We do not give a value for this parameter when
we call the method, Python provides it.
If we have a method which takes no arguments, then we still
have to have one argument – the self. See fun() in above simple
example.
This is similar to this pointer in C++ and this reference in Java.
When we call a method of this object as myobject.method(arg1,
arg2), this is automatically converted by Python into
MyClass.method(myobject, arg1, arg2) – this is all the special self
is about.
# creates a class named MyClass
class MyClass:
# assign the values to the MyClass attributes
number = 0
name = "noname"
def Main():
# Creating an object of the MyClass.Here, 'me' is the
object
me = MyClass()
# Accessing the attributes of MyClass, using the dot(.)
operator Output: Santosh 310
me.number = 310
me.name = "Santosh"
# str is an build-in function that, creates an string
print(me.name + " " + str(me.number))
# telling python that there is main in the program.
The __init__ method
The __init__ method is similar to constructors in C++ and Java. It is
run as soon as an object of a class is instantiated. The method is
useful to do any initialization you want to do with your object.
class Person:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
p = Person(‘Santosh')
p.say_hi()
Methods
Method is a bunch of code that is intended to perform a
particular task in your Python’s code.
Function that belongs to a class is called an
Method.
All methods require ‘self’ first parameter. If you have
coded in other OOP language you can think of ‘self’ as
the ‘this’ keyword which is used for the current object. It
unhides the current instance variable. ’self’ mostly work
like ‘this’.
‘def’ keyword is used to create a new method.
class Vector2D:
x = 0.0
y = 0.0
# Creating a method named Set
def Set(self, x, y):
self.x = x
self.y = y
def Main():
# vec is an object of class Vector2D
vec = Vector2D() Output: X: 5, Y:
6
# Passing values to the function Set
# by using dot(.) operator.
vec.Set(5, 6)
print("X: " + vec.x + ", Y: " + vec.y)
if __name__=='__main__':
Main()
Python In-built class functions
Inheritance
Inheritance is the capability of one class to derive or inherit the
properties from some another class. The benefits of inheritance
are:
It represents real-world relationships well.
It provides reusability of a code. We don’t have to write the same
code again and again. Also, it allows us to add more features to a
class without modifying it.
It is transitive in nature, which means that if class B inherits from
another class A, then all the subclasses of B would automatically
inherit from class A.
Inheritance is defined as a way in which a particular class
inherits features from its base class.
Base class is also knows as ‘Superclass’ and the class which
inherits from the Superclass is knows as ‘Subclass’
# Syntax for inheritance
class derived-classname(superclass-name)
class Person(object): # class Person: of Python 3.x is same as defined here.
def __init__(self, name): # Constructor
self.name = name
def getName(self): # To get name
return self.name
def isEmployee(self): # To check if this person is
employee
return False
# Inherited or Sub class (Note Person in bracket) OUTPUT:
class Employee(Person): Ram False
Shyam True
def isEmployee(self): # Here we return true
return True
# Driver code
emp = Person(“Ram") # An Object of Person
print(emp.getName(), emp.isEmployee())
emp = Employee(“Shyam") # An Object of Employee
Explore more on Inheritance Type
Thank You!