Unit 5
Unit 5
Unit 5
CCS 1500
Objectives
❖ Define OOP
❖ Define a class and the __init__() method
❖ Create and delete objects
❖ Modify and delete object properties
❖ Implement methods in a class
Object-Oriented Programming (OOP)
− a method of structuring a program by bundling related
properties and behaviors into individual objects
− models real-world entities as software objects that have
some data associated with them and can perform certain
operations
− objects are at the center of object-oriented
programming
❖ Python is OOP
− almost everything is an object, with their properties and
methods
❖ Example:
− an object could represent a person with properties like
a name, age, and address and behaviors such as
walking, talking, breathing, and running
− it could represent an email with properties like a
recipient list, subject, and body and behaviors like
adding attachments and sending
Create Classes
❖ Class
− like an object constructor
− a “blueprint” for creating objects
9
Create an Object
Delete an Object
11
The __init__() Method
12
The __init__() Method
− present in all classes
− executed when class is initiated
− used to assign values to the properties of an object or
other operations needed in the creation of an object
The self Parameter
15
self Parameter
− reference to the current instance of the class
▪ used to access the variables that belong to the class
▪ binds the attributes with the given arguments
− first parameter of any function in the class
▪ can use any other name
17
Modify and Delete Object
Properties
18
Modify and Delete Object Properties
value of per1.age
has been changed
20
Methods
− functions that belong to the object
Functions vs Methods
Function Method
doesn't need any object a function which is linked
and is independent with any object
can be directly called by called by the object's
its name name
used to pass or return the operates the data in a
data class
24
28
29
30
31
32
33