SlideShare a Scribd company logo
OOP in Python
A brief overview
CS 357
Dr. Eduardo Ceh
Topics
• Why do we need classes?
• Defining Classes
• Defining Objects
• Class variables vs Instance variables
• Methods
• Inheritance
• Polymorphism
Why do we need classes?
• Which field contains what type of information?
client = [“xxxxxxxxxxxxxxx",
“0000000000",
“xxxxxxxxx",
0]
The parts of a composite list can
be accessed via [index] but they
cannot be labeled (what do these
fields store?)
This isn’t immediately clear from looking at the program statements.
Object-Oriented Programming
You have learned structured programming
 Breaking tasks into subtasks
 Writing re-usable methods to handle tasks
We will now study Objects and Classes
 To build larger and more complex programs
 To model objects we use in the world
A class describes objects with the same
behavior. For example, a Car class
describes all passenger vehicles that have
a certain capacity and shape.
A class
class
In OOP a class is the definition of an object.
 In Python we define classes.
You can consider a class as a blueprint of an object
Using a class you instantiate objects
 These objects belong to the same class and therefore they contain the same
characteristics specified in the class
instances
objects
Defining a class
Diagram of a Class
Private Data
 We assume each object has its own private data that other objects cannot
directly access
 Methods of the public interface provide access to private data, while
hiding implementation details:
 This is called Encapsulation (Python has a loose definition of this)
Public Interface
 Each object has a set of methods available for other objects to use
Class
Private Data
(Variables/Attributes)
Public Interface
(Methods)
Variables “must”
be private
Methods must be
public
By the way, this is a class diagram
Let’s go to Google Colab
• Topics:
• Classes
• Writing classes in Python
• Class variables
• Class constructor
• Instance variables
• Instance methods
• Combining Class variables and Instance variables
• Class inheritance and Polymorphism
• Inheritance
• Polymorphism
Importing your classes as modules
• To use any package in your code, you must first make it accessible.
• You need to tell python to first import a module in your code so that
you can use it.
• If you have your own python files you want to import, you can use the
import statement as follows:
import my_file # assuming you have the file, my_file.py in the current directory.
# For files in other directories, provide path to that file, absolute or relative.
Importing our Python class
The file we want to import
Having the class Client
Importing our Python class
The file where we
want to use the Class
The output if we run
the main.py
Something is NOT right.
__name__ == “__main__”
Magic method that allows or prevents parts of code
from being run when the modules are imported
When the Python interpreter reads a file, the __name__
variable is set as __main__ if the module being run, or as
the module's name if it is imported.
Importing our Python class
Now, the code in client.py that is not part of the class is not executed.
Importing elements from the file
File Class
Introduction to Object Oriented Programming in Python.pptx
Ad

More Related Content

Similar to Introduction to Object Oriented Programming in Python.pptx (20)

object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
KavitaHegde4
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
KavitaHegde4
 
basic concepts of object oriented in python
basic concepts of object oriented in pythonbasic concepts of object oriented in python
basic concepts of object oriented in python
deepalishinkar1
 
slides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptxslides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptx
debasisdas225831
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
MH Abid
 
arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Saiful Islam Sany
 
Class and Objects in python programming.pptx
Class and Objects in python programming.pptxClass and Objects in python programming.pptx
Class and Objects in python programming.pptx
Rajtherock
 
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.pptJava Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
akashsachu221
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
baabtra.com - No. 1 supplier of quality freshers
 
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON  OBJECT-ORIENTED PROGRAMMING.pptxPYTHON  OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
Object oriented programming design and implementation
Object oriented programming design and implementationObject oriented programming design and implementation
Object oriented programming design and implementation
afsheenfaiq2
 
Object oriented programming design and implementation
Object oriented programming design and implementationObject oriented programming design and implementation
Object oriented programming design and implementation
afsheenfaiq2
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
KavitaHegde4
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
KavitaHegde4
 
basic concepts of object oriented in python
basic concepts of object oriented in pythonbasic concepts of object oriented in python
basic concepts of object oriented in python
deepalishinkar1
 
slides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptxslides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptx
debasisdas225831
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
MH Abid
 
arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Class and Objects in python programming.pptx
Class and Objects in python programming.pptxClass and Objects in python programming.pptx
Class and Objects in python programming.pptx
Rajtherock
 
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.pptJava Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
akashsachu221
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON  OBJECT-ORIENTED PROGRAMMING.pptxPYTHON  OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
Object oriented programming design and implementation
Object oriented programming design and implementationObject oriented programming design and implementation
Object oriented programming design and implementation
afsheenfaiq2
 
Object oriented programming design and implementation
Object oriented programming design and implementationObject oriented programming design and implementation
Object oriented programming design and implementation
afsheenfaiq2
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 

Recently uploaded (20)

Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIHlecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
Abodahab
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIHlecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
Abodahab
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Ad

Introduction to Object Oriented Programming in Python.pptx

  • 1. OOP in Python A brief overview CS 357 Dr. Eduardo Ceh
  • 2. Topics • Why do we need classes? • Defining Classes • Defining Objects • Class variables vs Instance variables • Methods • Inheritance • Polymorphism
  • 3. Why do we need classes? • Which field contains what type of information? client = [“xxxxxxxxxxxxxxx", “0000000000", “xxxxxxxxx", 0] The parts of a composite list can be accessed via [index] but they cannot be labeled (what do these fields store?) This isn’t immediately clear from looking at the program statements.
  • 4. Object-Oriented Programming You have learned structured programming  Breaking tasks into subtasks  Writing re-usable methods to handle tasks We will now study Objects and Classes  To build larger and more complex programs  To model objects we use in the world A class describes objects with the same behavior. For example, a Car class describes all passenger vehicles that have a certain capacity and shape.
  • 5. A class class In OOP a class is the definition of an object.  In Python we define classes. You can consider a class as a blueprint of an object Using a class you instantiate objects  These objects belong to the same class and therefore they contain the same characteristics specified in the class instances objects
  • 7. Diagram of a Class Private Data  We assume each object has its own private data that other objects cannot directly access  Methods of the public interface provide access to private data, while hiding implementation details:  This is called Encapsulation (Python has a loose definition of this) Public Interface  Each object has a set of methods available for other objects to use Class Private Data (Variables/Attributes) Public Interface (Methods) Variables “must” be private Methods must be public By the way, this is a class diagram
  • 8. Let’s go to Google Colab • Topics: • Classes • Writing classes in Python • Class variables • Class constructor • Instance variables • Instance methods • Combining Class variables and Instance variables • Class inheritance and Polymorphism • Inheritance • Polymorphism
  • 9. Importing your classes as modules • To use any package in your code, you must first make it accessible. • You need to tell python to first import a module in your code so that you can use it. • If you have your own python files you want to import, you can use the import statement as follows: import my_file # assuming you have the file, my_file.py in the current directory. # For files in other directories, provide path to that file, absolute or relative.
  • 10. Importing our Python class The file we want to import Having the class Client
  • 11. Importing our Python class The file where we want to use the Class The output if we run the main.py Something is NOT right.
  • 12. __name__ == “__main__” Magic method that allows or prevents parts of code from being run when the modules are imported When the Python interpreter reads a file, the __name__ variable is set as __main__ if the module being run, or as the module's name if it is imported.
  • 13. Importing our Python class Now, the code in client.py that is not part of the class is not executed.
  • 14. Importing elements from the file File Class

Editor's Notes

  • #4: It is difficult to understand and update a program that consists of a large collection of methods. To overcome this problem, computer scientists invented object-oriented programming, tasks are solved by collaborating objects When you develop an object-oriented program, you create your own objects that describe what is important in your application.
  • #14: If you only use “import Client”, you are importing EVERYTHING from that file. Therefore, you need to specify what classes or methods you want to use. For example, Client.Client()