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

Introduction to Object Oriented Programming in Python.pptx

  • 1.
    OOP in Python Abrief overview CS 357 Dr. Eduardo Ceh
  • 2.
    Topics • Why dowe need classes? • Defining Classes • Defining Objects • Class variables vs Instance variables • Methods • Inheritance • Polymorphism
  • 3.
    Why do weneed 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 havelearned 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 OOPa 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
  • 6.
  • 7.
    Diagram of aClass 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 toGoogle 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 classesas 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 Pythonclass The file we want to import Having the class Client
  • 11.
    Importing our Pythonclass The file where we want to use the Class The output if we run the main.py Something is NOT right.
  • 12.
    __name__ == “__main__” Magicmethod 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 Pythonclass Now, the code in client.py that is not part of the class is not executed.
  • 14.
    Importing elements fromthe 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()