[PDF] [PDF] Object-oriented Programming with Python - Indian Statistical Institute

2 Features of OOP Classes and Objects Encapsulation Constructor Polymorphism Inheritance Special features Malay Bhattacharyya Computing Laboratory 



Previous PDF Next PDF





[PDF] Object-Oriented Design with Python

programming with python • Introduces Python's special methods to realize class definition, inheritance, multiple inheritance, accessibility, polymorphism 



[PDF] encapsulation, inheritance, polymorphism

Polymorphism and Wrapping builtin functions encapsulation, inheritance, polymorphism L-26 framework languages: Java, Python A framework is a 



[PDF] Download Object Oriented Python Tutorial - Tutorialspoint

One good example of polymorphism is constructor overloading in classes Page 11 OOP in Python 5 Object-Oriented Python



[PDF] Object-oriented Programming with Python - Indian Statistical Institute

2 Features of OOP Classes and Objects Encapsulation Constructor Polymorphism Inheritance Special features Malay Bhattacharyya Computing Laboratory 



[PDF] Inheritance Polymorphism Overloading Overriding - gktcs

use the second addition method, as python does not supports method overloading ❑ We may define many method of same name and different argument but



[PDF] Introduction to Python Part 3: Object-Oriented Programming

7 oct 2009 · Part 3: Object- Oriented Programming Polymorphism Introspection Definition Everything Is an Object Part III Python's Object System 



[PDF] Cooperative Object-Oriented Programming in Python - Fayetteville

hiding; it has full support in polymorphism and inheritance Therefore, Python fits the definition of object-oriented programming languages On the other hand, 



[PDF] Object Oriented Programming through Python Laboratory - IARE

Understand the concepts of inheritance, polymorphism and overriding LIST OF EXPERIMENTS Week-1 BASICS OF PYTHON Write Python programs for the 

[PDF] polynomial function examples

[PDF] polynomial functions test pdf

[PDF] polynomial functions worksheet

[PDF] polynomial operations

[PDF] polynomial problems and answers pdf

[PDF] polynomials class 9 pdf ncert

[PDF] polynomials pdf class 10

[PDF] pop zip code

[PDF] popular fast food restaurants in france

[PDF] population aging is a global phenomenon

[PDF] population aging rate by country

[PDF] population growth europe

[PDF] population of eu countries 2019

[PDF] population of europe

[PDF] population of europe in 1300

OutlineObject-oriented Programming (OOP)Features of OOP

Computing Laboratory

Object-oriented Programming with Python

Malay Bhattacharyya

Assistant Professor

Machine Intelligence Unit

Indian Statistical Institute, Kolkata

January, 2021

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

1Object-oriented Programming (OOP)2Features of OOPClasses and Objects

Encapsulation

Constructor

Polymorphism

Inheritance

Special features

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Object-oriented Programming (OOP)

Objects are the basic runtime entities in an object-oriented system.

Objects encapsulate data and method.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Class and Object

Classes are the building blocks in OOP

A class is like a template that describes the behaviors/states that objects of its type support. It can be considered as a user-dened entity that can combine data and functionality together.A class contains:

Data members known asattributesof the classMember functions known asmethodsof the classAn object is an instance of a class

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Class and Object

Classes are the building blocks in OOP

A class is like a template that describes the behaviors/states that objects of its type support. It can be considered as a user-dened entity that can combine data and functionality together.A class contains:

Data members known asattributesof the classMember functions known asmethodsof the classAn object is an instance of a class

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Class and Object

Classes are the building blocks in OOP

A class is like a template that describes the behaviors/states that objects of its type support. It can be considered as a user-dened entity that can combine data and functionality together.A class contains:

Data members known asattributesof the classMember functions known asmethodsof the classAn object is an instance of a class

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Empty class

Dening an empty class in Python:

class PythonProgrammer: pass

Note:passis used as a placeholder with no errors getting created.Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Object of an empty class

Creating an object of an empty class:

PP = PythonProgrammer() # InstantiationWhat is the output of the following code? class PythonProgrammer: pass

PP = PythonProgrammer()

print(PP)Output: <__main__.PythonProgrammer object at 0x7f3d6b7f4b70>

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Object of an empty class

Creating an object of an empty class:

PP = PythonProgrammer() # InstantiationWhat is the output of the following code? class PythonProgrammer: pass

PP = PythonProgrammer()

print(PP)Output: <__main__.PythonProgrammer object at 0x7f3d6b7f4b70>

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Object of an empty class

Creating an object of an empty class:

PP = PythonProgrammer() # InstantiationWhat is the output of the following code? class PythonProgrammer: pass

PP = PythonProgrammer()

print(PP)Output: <__main__.PythonProgrammer object at 0x7f3d6b7f4b70>

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Object of an empty class

What is the output of the following code?

class PythonProgrammer: pass

PP1 = PythonProgrammer()

print(PP1)

PP2 = PythonProgrammer()

print(PP2)

PP1 == PP2Output:

<__main__.PythonProgrammer object at 0x7f3d6b7f4da0> <__main__.PythonProgrammer object at 0x7f3d6b7f4e48> False Note:ThoughPP1andPP2are instances of the same class, they represent two distinct objects in memory.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Object of an empty class

What is the output of the following code?

class PythonProgrammer: pass

PP1 = PythonProgrammer()

print(PP1)

PP2 = PythonProgrammer()

print(PP2)

PP1 == PP2Output:

<__main__.PythonProgrammer object at 0x7f3d6b7f4da0> <__main__.PythonProgrammer object at 0x7f3d6b7f4e48> False Note:ThoughPP1andPP2are instances of the same class, they represent two distinct objects in memory.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Using an empty class

Using an empty class in Python:

class PythonProgrammer: pass

PP1 = PythonProgrammer()

PP1.fullname = "Malay Bhattacharyya"

PP1.height = 5.6

PP1.age = 38

PP2 = PythonProgrammer()

PP2.fullname = "Mandar Mitra"

PP2.height = 5.7

PP2.age = 49

print("Average age:", (PP1.age + PP2.age) / 2)Output:Average age: 43.5Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Using an empty class

Using an empty class in Python:

class PythonProgrammer: pass

PP1 = PythonProgrammer()

PP1.fullname = "Malay Bhattacharyya"

PP1.height = 5.6

PP1.age = 38

PP2 = PythonProgrammer()

PP2.fullname = "Mandar Mitra"

PP2.height = 5.7

PP2.age = 49

print("Average age:", (PP1.age + PP2.age) / 2)Output:Average age: 43.5Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Standard class

Dening a standard class in Python:

class PythonProgrammer: fullname = "Guido van Rossum" height = 5.66 age = 64 def feature(self):

print("Creator of Python is: " + self.fullname)Attributes are always public and can be accessed using the dot

(.) operator.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Standard class { The use ofselfClass methods must take the rst parameter asself.Python provides a value to theselfparameter not the user.Theselfparameter value is used for referencing. An object

is linked to the class through this.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Standard class { The use ofselfClass methods must take the rst parameter asself.Python provides a value to theselfparameter not the user.Theselfparameter value is used for referencing. An object

is linked to the class through this.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Standard class { The use ofselfClass methods must take the rst parameter asself.Python provides a value to theselfparameter not the user.Theselfparameter value is used for referencing. An object

is linked to the class through this.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Object of a standard class

Creating an object of a standard class:

PP = PythonProgrammer() # Instantiation

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Using a standard class

Using a standard class in Python:

class PythonProgrammer: fullname = "Guido van Rossum" height = 5.66 age = 64 def feature(self): print("Creator of Python is: " + self.fullname)

PP = PythonProgrammer()

PP.feature() # Method via object

print("Height: " + str(PP.height)) # Attribute via object print("Age: " + str(PP.age)) # Attribute via objectOutput:Creator of Python is: Guido van Rossum

Height: 5.66

Age: 64

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Using a standard class

Using a standard class in Python:

class PythonProgrammer: fullname = "Guido van Rossum" height = 5.66 age = 64 def feature(self): print("Creator of Python is: " + self.fullname)

PP = PythonProgrammer()

PP.feature() # Method via object

print("Height: " + str(PP.height)) # Attribute via object print("Age: " + str(PP.age)) # Attribute via objectOutput:Creator of Python is: Guido van Rossum

Height: 5.66

Age: 64

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Immutable classes and objects

Immutable classes are Python classes whose objects can not be modied once created, and such objects are known as immutable objects. Any modication in immutable objects results into a new object.Objects of built-in types like int, oat, bool, str, tuple, unicode are immutable in Python.Objects of built-in types like list, set, dict are mutable.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Immutable classes and objects

Immutable classes are Python classes whose objects can not be modied once created, and such objects are known as immutable objects. Any modication in immutable objects results into a new object.Objects of built-in types like int, oat, bool, str, tuple, unicode are immutable in Python.Objects of built-in types like list, set, dict are mutable.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Immutable classes and objects

Immutable classes are Python classes whose objects can not be modied once created, and such objects are known as immutable objects. Any modication in immutable objects results into a new object.Objects of built-in types like int, oat, bool, str, tuple, unicode are immutable in Python.Objects of built-in types like list, set, dict are mutable.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Encapsulation

An object = data + method

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

A constructor is used to initialize the object's state Constructors are methods that are useful for any kind of

initialization you want to perform with the objects.Note:A constructor is executed as soon as an object of a class is

instantiated.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

A constructor is used to initialize the object's state Constructors are methods that are useful for any kind of

initialization you want to perform with the objects.Note:A constructor is executed as soon as an object of a class is

instantiated.

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Dening a constructor:

class PythonProgrammer: def __init__(self, fullname, height, age): self.fullname = fullname self.height = height self.age = ageself.fullname = fullnamecreates the attributefullname

and assigns the value of the parameterfullnameto it.self.height = heightcreates the attributeheightand

assigns the value of the parameterheightto it.self.age = agecreates an attribute calledageand assigns

the value of the parameterageto it.Malay BhattacharyyaComputing Laboratory OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Dening a constructor:

class PythonProgrammer: def __init__(self, fullname, height, age): self.fullname = fullname self.height = height self.age = ageself.fullname = fullnamecreates the attributefullname

and assigns the value of the parameterfullnameto it.self.height = heightcreates the attributeheightand

assigns the value of the parameterheightto it.self.age = agecreates an attribute calledageand assigns

the value of the parameterageto it.Malay BhattacharyyaComputing Laboratory OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Using a constructor:

class PythonProgrammer: def __init__(self, fullname, height, age): self.fullname = fullname self.height = height self.age = age def show(self): print(self.fullname) PP = PythonProgrammer("Guido van Rossum", 5.66, 64) PP.show()Output:Guido van RossumMalay BhattacharyyaComputing Laboratory OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Using a constructor:

class PythonProgrammer: def __init__(self, fullname, height, age): self.fullname = fullname self.height = height self.age = age def show(self): print(self.fullname) PP = PythonProgrammer("Guido van Rossum", 5.66, 64) PP.show()Output:Guido van RossumMalay BhattacharyyaComputing Laboratory OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Using a constructor:

class PythonProgrammer: def __init__(self, fullname): self.fullname = fullname def include(self, height): self.height = height def show(self): print(self.fullname) print(self.height)

PP = PythonProgrammer("Guido van Rossum")

PP.include(5.66)

PP.show()Output:Guido van Rossum

5.66

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Using a constructor:

class PythonProgrammer: def __init__(self, fullname): self.fullname = fullname def include(self, height): self.height = height def show(self): print(self.fullname) print(self.height)

PP = PythonProgrammer("Guido van Rossum")

PP.include(5.66)

PP.show()Output:Guido van Rossum

5.66

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Using a constructor:

class PythonProgrammer: def __init__(self, fullname): self.fullname = fullname # We cannot write return(self.fullname) here def include(self, height): self.height = height return(self.height) def send(self): return(self.fullname)

PP = PythonProgrammer("Guido van Rossum")

print(PP.send()) print(PP.include(5.66))Output:Guido van Rossum 5.66

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Constructor

Using a constructor:

class PythonProgrammer: def __init__(self, fullname): self.fullname = fullname # We cannot write return(self.fullname) here def include(self, height): self.height = height return(self.height) def send(self): return(self.fullname)

PP = PythonProgrammer("Guido van Rossum")

print(PP.send()) print(PP.include(5.66))Output:Guido van Rossum 5.66

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Polymorphism

Polymorphism is the condition of occurrence in dierent forms

Python allows polymorphism in many dierent forms:At the operator level (also known as operator overloading)

At the function level (also known as function overloading) At the class level (also known as method overloading)

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Polymorphism

Example of operator overloading:

i, j = 2000, 20 print(i + j) s1 = "Computing" s2 = "Lab" print(s1 + " " + s2)Output:2020

Computing Lab

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Polymorphism

Example of operator overloading:

i, j = 2000, 20 print(i + j) s1 = "Computing" s2 = "Lab" print(s1 + " " + s2)Output:2020

Computing Lab

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Polymorphism

Example of function overloading:

print(len("Python")) print(len(["ver1", "ver2", "ver3"])) print(len({"Creator": "Guido", "Nationality": "Dutch"}))Output:6 3 2

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOP

Polymorphism

Example of function overloading:

print(len("Python")) print(len(["ver1", "ver2", "ver3"])) print(len({"Creator": "Guido", "Nationality": "Dutch"}))Output:6 3 2

Malay BhattacharyyaComputing Laboratory

OutlineObject-oriented Programming (OOP)Features of OOPquotesdbs_dbs14.pdfusesText_20