0% found this document useful (0 votes)
5 views18 pages

L1_OOP Python. Classes

Uploaded by

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

L1_OOP Python. Classes

Uploaded by

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

Object-Oriented Programming

(OOP) in Python
Classes and Objects
Intro OOP
Object-oriented programming (OOP) is a method of
structuring a program by bundling related properties and
behaviors into individual objects.
Conceptually, objects are like the components of a
system. An object contains data (properties) and
behavior(methods)
For instance, an object could represent a person with
properties like a name, age, and address and behaviors
such as walking, talking, breathing, and running.
Or it could represent an email with properties like a
recipient list, subject, and body and behaviors like adding
attachments and sending.
Define a Class in Python
Classes are used to create user-defined data structures.
Classes define functions called methods, which identify
the behaviors and actions that an object created from the
class can perform with its data.
A class is a blueprint for how something should be
defined. It doesn’t actually contain any specific data.
An instance is an object that is built from a class and
contains real data.
Class creates a user-defined data structure, which holds
its own data members and member functions, which can
be accessed and used by creating an instance of that
class. A class is like a blueprint for an object.
Python implementation

All class definitions start with the class keyword,


which is followed by the name of the class and a
colon. Any code that is indented below the class
definition is considered part of the class’s body.

Syntax:
class ClassName:
# Statement

Note: Python class names are written in CapitalizedWords notation


by convention.
Some points on Python class:
• Classes are created by keyword class.
• Attributes are the variables that belong to a
class.
• Attributes are always public and can be
accessed using the dot (.) operator. Eg.:
Myclass.Myattribute
Defining a class
# Python program to demonstrate defining
# a class
class Dog:
pass

The pass Statement


class definitions cannot be empty, but if you for
some reason have a class definition with no
content, put in the pass statement to avoid
getting an error.
Class Objects
An Object is an instance of a Class.
An object consists of :
• State: It is represented by the attributes of an object.
It also reflects the properties of an object.
• Behaviour: It is represented by the methods of an
object. It also reflects the response of an object to
other objects.
• Identity: It gives a unique name to an object and
enables one object to interact with other objects.
Declaring Objects (Also called
instantiating a class)
When an object of a class is created, the class is said
to be instantiated. All the instances share the
attributes and the behavior of the class. But the
values of those attributes, i.e. the state are unique
for each object. A single class may have any number
of instances.
Syntax:
obj = ClassName()
print(obj.atrr)
Declaring an object
# Python program to demonstrate instantiating a class
class Dog:
# attribute
attr1 = "mammal"
attr2 = "dog"
# A sample method
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)
# main code
# Object instantiation
Rodger = Dog()
# Accessing class attributes and method through
objects
print(Rodger.attr1)
Rodger.fun()
The __init__() Function
All classes have a function called __init__(), which is
always executed when the class is being initiated.
Use the __init__() function to assign values to
object properties or other operations that are
necessary to do when the object is being created:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Ali", 20)
p2 = Person("AmirA", 31)
print(p1.name)
print(p2.age)
The self Parameter
The self parameter is a reference to the current
instance of the class, and is used to access variables
that belongs to the class.
It does not have to be named self , you can call it
whatever you like, but it has to be the first
parameter of any function in the class:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age

def myfunc(abc):
print("Hello my name is " + abc.name)

def myfunc1(abc):
print("I am " + str(abc.age))

p1 = Person("Ali", 36)
p2= Person("Amira", 31)

p1.myfunc()
p2.myfunc1()
Modify Object Properties
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello" + self.name)

p1 = Person("Ali", 20)
p1.age = 40
Delete Object Properties
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello my name is " + self.name)

p1 = Person(“Ali", 20)
del p1.age
print(p1.age)
Delete Objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Ali", 20)
p2 = Person("AmirA", 31)
del p2
print(p1.name)
print(p2.age)
Self training

• Follow link: Python Basics: Chapter 10 – Object-Oriented


Programming. clck.ru/33sUsy
[https://realpython.com/quizzes/pybasics-oop/viewer/]
• Follow link: Python Classes And Objects Quiz.
clck.ru/33sV72 [https://www.proprofs.com/quiz-
school/story.php?title=mjqyoda1mqr4a4]
• Complete tasks
https://www.w3schools.com/python/python_cl
asses.asp
https://www.geeksforgeeks.org/python-classes-
and-objects/
https://realpython.com/python3-object-oriente
d-programming/
https://www.programiz.com/python-programmi
ng/class
https://www.tutorialspoint.com/python/
python_classes_objects.htm

You might also like