0% found this document useful (0 votes)
2 views33 pages

Unit 5

This document covers the fundamentals of Object-Oriented Programming (OOP) in Python, including the definition of classes, the __init__() method, and how to create, modify, and delete objects. It explains the importance of the 'self' parameter and the distinction between functions and methods. The content serves as a guide for understanding how to structure programs using OOP principles.

Uploaded by

kayelpat2708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views33 pages

Unit 5

This document covers the fundamentals of Object-Oriented Programming (OOP) in Python, including the definition of classes, the __init__() method, and how to create, modify, and delete objects. It explains the importance of the 'self' parameter and the distinction between functions and methods. The content serves as a guide for understanding how to structure programs using OOP principles.

Uploaded by

kayelpat2708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Using Classes

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

❖ An object that belongs to a class is an instance of that


class
Create a Class
Create and Delete 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

use the del keyword


Syntax: del <object>.<property>

per1.age is already deleted


Implement Methods

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

You might also like