Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CPE551WS: Python Programming

Mukund Iyengar
ECE Department
miyengar@stevens
Agenda:

Object Oriented Programming


- What are they?
- To OOP or not in Python
- Classes and objects in Python
- The ‘self’ argument
- Inheritance and polymorphism
- Some examples
What is OOP
Using classes and objects to conceptualize data

Conceptualize Create “Objects” Derive new


(i.e., class) based on class definitions (inherit)
Basic semantics
Using class definitions and creating objects

The Semantics
What it all boils down to:
#Create Class
class Simple:
pass

#Create Object
obj = Simple()
OOP at 30,000 feet
“Self” Awareness

C
raiseSalary(self)
printSalary(self)

“self”

“self” “self”

Ob1 Ob2 Ob3


Examples: how it works
Anatomy of a class definition

class Employee: ; Class Name


def __init__(self, name, sal,…):
self.name = name ; Constructor & Attributes
self.salary = sal

def function1(self, …):


...

def function2(self, …): ; Class Functionalities


...
Customize objects after definition

class Employee:
...

E1 = Employee()
E1.title = “Manager”

You can add a new attribute to an object, even if other objects will
never have that attribute and neither does the class definition!
Python rarely uses OOP

Why oh why?

Python is a scripted language meant for rapid prototyping, and less for creating software suits in
the traditional waterfall model
Software “Stacks”

Feature 1 Feature 2 Something


new...

Middle level: C++, C, Java

Lowest level: Assembly & Drivers

You might also like