0% found this document useful (0 votes)
31 views14 pages

L10 ClassesAndObjects

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views14 pages

L10 ClassesAndObjects

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ED5340 - Data Science: Theory

and Practise
L10 - Classes and Objects

Ramanathan Muthuganapathy ([Link]


Course web page: [Link]
Moodle page: Available at [Link]
Classes and objects
A ‘rough’ idea of what they are

• Classes are similar to structures in C


• Objects are similar to variables that access the member of the structure.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Classes and objects

• Class contains data and methods that can access or manipulate this data.
• Data is typically accessed through the methods (data protection).
• The methods are accessed through ‘object’ instantiation.
• Broadly comes under object-oriented programming (others being functional,
structural programming).

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Class details
Name, data, methods (member functions) - L10_class_example.py
class StudentDetail:

def datainput(self, n, r, s):


[Link] = n
[Link] = r
[Link] = s

def printout(self):
print([Link], [Link], [Link])

s1 = StudentDetail( ) #Object instantiation


[Link]('Ram', 12, 3)
[Link]( )
Class - Using constructor
Name, data, methods (member functions) - L10_class_example.py

class StudentDetail:

def __init__(self, n='', r=1, s=1):


[Link] = n
[Link] = r
[Link] = s

def printout(self):
print([Link], [Link], [Link])

s2 = StudentDetail('Ram', 12, 3 ) #Object instantiation


[Link]( )
Public Data
Name, data, methods (member functions)
class StudentDetail:

def datainput(self, n, r, s):


[Link] = n
[Link] = r
[Link] = s

def printout(self):
print([Link], [Link], [Link])

s1 = StudentDetail( ) #Object instantiation


[Link]('Ram', 12, 3)
[Link]( )
print('name = ', [Link], 'rollno = ', [Link], 'sem = ', [Link])
Private Data
Name, data, methods (member functions) - L10_class_example.py
class StudentDetail:

def datainput1(self, n, r, s):


self._name1 = n
self._rollno1 = r
self._sem1 = s

def printout(self):
print([Link], [Link], [Link])

s2 = StudentDetail( ) #Object instantiation


s2.datainput1('Shyam', 23, 34)
[Link]( )
print('name = ', s2._name1)
Constructor
L10_class_constructor.py
class StudentDetail:

#Constructor
def __init__(self, n='R', r=1, s=1):
self._name = n
self._rollno = r
self._sem = s

#Printing the data


def printout(self):
print('name = ', self._name, ", " , 'roll no = ', self._rollno, ", " , 'sem = ', self._sem)

#destructor
def __del__(self):
print('Del obj' + str(self))

s1 = StudentDetail()
[Link]()

s1 = StudentDetail('Ram')
[Link]()

s1 = StudentDetail('Raman', 23)
[Link]()

s1 = StudentDetail('Ramana', 23, 5)
[Link]()
Class variables and methods
L10_cmv.py

• One variable shared across all objects


• ‘self’ should not be used
• syntax: [Link]
• similar rules for class methods ([Link]( ))
• similar to static members in C++

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Notation - Convention
L10_pvt_example.py

• Class name starts with Caps


• single _ for notionally private variable
• __ (dunderscore) for strictly private
• __ used in data as well as methods (e.g.?)

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Operator overloading
L10_class_complex.py

• a + b already defined
• Operator overloading is done for user defined classes, for e.g. class Complex.
• def add_comp(self, other):
• c1.add_comp(c2)
• def __add__(self, other)
• c1 + c2 #(More intuitive usage)

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Operators that can be overloaded

• __sub__
• __mul__
• ………… (find out the list of operators that can be overloaded)

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


CW: Do the + and - for the
Complex class.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Dynamic creation of attributes
L10_dy_creation.py

class Passbook:

pass

p1 = Passbook( )

p1._name = ‘Raman’

p1._number = 1234

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras

You might also like