0% found this document useful (0 votes)
111 views55 pages

Python Classes and Objects

Uploaded by

Priya
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)
111 views55 pages

Python Classes and Objects

Uploaded by

Priya
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/ 55

CHAPTER 10

Python Classes
and objects
• Understand the fundamental
concepts of Object Oriented
Programming like: Classes, Objects,
Constructor and Destructor.
• Gain the knowledge of creating
classes and objects in Python.
• Create classes with Constructors.
• Write complex programs in Python
using classes.
Introduction
Python is an Object
Oriented Programming
language.
Classes and Objects are the
key features of Object
Oriented Programming.
Class is the main
building block in Python.
Object is a collection of
data and function that act
on those data.
Class is a
template for
the object.
class MyClass:
x=5
p1 = MyClass()
print(p1.x)
Objects are also
called as instances
of a class or class
variable.
In Python,
everything is
an object.
Defining classes
In Python, a class is
defined by using the
keyword class.
Every class has a unique
name followed by a colon
( : ).
Syntax
class class_name:
statement_1
statement_2
…………..
…………..
statement_n
Statement in a class
definition may be a
variable declaration,
decision control, loop or
even a function definition.
Variables defined inside
a class are called as
“Class Variable”
Functions are called as
“Methods”.
Class variable and
methods are together
known as members
of the class.
The class members should
be accessed through
objects or instance of class.
A class can be defined
anywhere in a Python
program.
Creating
Objects
Once a class is created, next
you should create an object
or instance of that class.
The process of creating
object is called as “Class
Instantiation”.
Syntax
Object_name = class_name( )
class
Sample:
Accessing Class
Members
Any class member ie.
class variable or method
(function) can be
accessed by using object
with a dot ( . ) operator.
Syntax
Object_name . class_member
Example
class Sample:
#class variables
x, y = 10, 20
S=Sample( ) # class
instantiation
print("Value of x = ", S.x)
print("Value of y = ", S.y)
Class name
Sample
Class variables
or
x y member variables

10 20

S object
S.x S.y
10 20
+

30
Class Methods
The class method must have the
first argument named as self.
No need to pass a value for this
argument when we call the
method.
Python provides its value
automatically.
Even if a method takes no
arguments, it should be defined
with the first argument called self.
If a method is defined to accept
only one argument it will take it as
two arguments
ie. self and the defined argument.
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 Student:
def process(self):
print("Hello
Excel:")
S=Student()
class St:
m1, m2, m3 = 45, 91, 71 #class
variable
def process(self): #class method
sum = St.m1 + St.m2 + St.m3
avg = sum/3
print("Total Marks = ", sum)
print("Average Marks = ",
avg)
Class name
St
Class variables
or
m1 m2 m3 member variables

45 91 71

S object
St.m1 St.m2 St.m3

45 + 91 + 71

207 / 3 69.0
class Student:
def process(self,n):
print ("the value of N
is:",n)
S=Student()
x = int(input("Enter a
class Student:
def process(self,m,n)
print ("the value of M and N
is:",m,n)
S=Student()
x = int(input("Enter a value: "))
y = int(input("Enter a value: "))
S.process(x,y)
Constructor
and
Destructor
in Python
Constructor is
a special type of
function that is called
automatically whenever an
object of that class is
created.
Constructor is the special function that is
automatically executed when an object of a
class is created.
In Python, there is a special function called
“init” which act as a Constructor. It must begin
and end with double underscore.
This function will act as an ordinary function;
but only difference is, it is executed
automatically when the object is created.
class Vehicle:
car = Vehicle()
The __init__ function is
called a constructor, or
initializer, and is
automatically called when
you create a new instance of
a class.
__init__
This constructor function
can be defined with or
without arguments.
This method is used to
initialize the class variables.
class Car:
def __init__(self):
print('Hello world')
car = Car()
class Vehicle:
def __init__(self):
print('Vehicle created.')
def __del__(self):
print('Destructor called, vehicle
deleted.')
car = Vehicle()
print('Hello world')
class Employee:
def __init__(self,name,id):
self.id = id;
self.name = name;
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
emp1 = Employee("John",101)
emp2 = Employee("David",102)
emp1.display();
emp2.display();
Public and Private
Data Members
The variables which are defined inside
the class is public by default. These
variables can be accessed anywhere in
the program using dot operator.
A variable prefixed with double
underscore becomes private in nature.
These variables can be accessed only
within the class.
N1 public
__N2 private

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy