SlideShare a Scribd company logo
CHAPTER - 05
CLASSES AND OBJECTS
Unit I
Programming and Computational
Thinking (PCT-2)
(80 Theory + 70 Practical)
DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)
Department of Computer Science, Sainik School Amaravathinagar
Cell No: 9431453730
Praveen M Jigajinni
Prepared by
Courtesy CBSE
Class XII
INTRODUCTION
INTRODUCTION
This chapter deals with classes in
Python. As Python is fully object-oriented,
you can define your own Classes, inherit
from your own or built-in classes, and
instantiate the classes that you've defined.
But before we start with our study on
classes let us understand about namespaces
and scope rules in Python.
NAMESPACES
These consist of functions such as max() ,
min() and built-in exception names. This
namespace is created when the Python
interpreter starts up, and is never deleted.
The built-in names actually also live in a
module called__ builtin__
1. BULIT IN NAMESPACE
The global namespace for a module is
created when the module definition is read in
and normally lasts until the interpreter quits.
The statements executed by the top-level
invocation of the interpreter, either read
from a script file or interactively, are
considered to be part of a module
called__main__ and they have their own
global namespace.
2. GLOBAL NAMES IN A MODULE
The global namespace for a module is
created when the module definition is read in
and normally lasts until the interpreter quits.
The statements executed by the top-level
invocation of the interpreter, either read
from a script file or interactively, are
considered to be part of a module
called__main__ and they have their own
global namespace.
3. LOCAL NAMES IN A FUNCTION INVOCATION
The local namespace for a function is
created when the function is called, and
deleted when the function returns or raises
an exception that is not handled within the
function. Even each recursive invocation has
its own local namespace.
3. LOCAL NAMES IN A FUNCTION INVOCATION
The local namespace for a function is
created when the function is called, and
deleted when the function returns or raises
an exception that is not handled within the
function. Even each recursive invocation has
its own local namespace.
If we talk about classes and objects, the set
of attributes of an object also form a
namespace.
contd…
3. LOCAL NAMES IN A FUNCTION INVOCATION
It must be noted that there is absolutely
no relation between names in different
namespaces. Two different modules may
both define same function without any
confusion because the functions are prefixed
with the module name. That means
module1.cmp() has no relation with
module2.cmp().
3. LOCAL NAMES IN A FUNCTION INVOCATION
SCOPE RULES
A scope is a region of a Python program
where a namespace is directly accessible. The
location where the names are assigned in the
code determines the scope of visibility of the
name in that code.
SCOPE RULES
LEGB RULE
According to this rule, when a name is
encountered during the execution of the
program, it searches for that name in the
following order:
L. Local
E. Enclosing functions
G. Global (module)
B. Built-in (Python)
LEGB RULE
L. Local - It first makes a local search i.e. in
current def statement. The import
statements and function definitions bind
the module or function name in the local
scope. In fact, all operations that introduce
new names use the local scope.
LEGB RULE – L Local
E. Enclosing functions - It searches in all
enclosing functions, form inner to outer.
LEGB RULE – E. Enclosing functions
G. Global (module) - It searches for global
modules or for names declared global in a
def within the file.
LEGB RULE – G. Global (module)
B. Built-in (Python) - Finally it checks for any
built in functions in Python.
LEGB RULE – B. Built-in (Python)
CLASSES AND OBJECTS
DEFINING CLASSES AND OBJECTS
Python is an object-oriented programming
language.
Object-oriented programming (OOP)
focuses on creating reusable patterns of
code.
A blueprint or prototypes created by a
programmer for an object is called class.
Class is a container of attributes and
methods. Its an Abstract Data Type (ADT)
Contd…
WHAT IS CLASS?
A USER-DEFINED PROTOTYPE for an
object that defines a set of attributes that
characterize any object of the class. The
attributes are data members (class variables
and instance variables) and methods,
accessed via dot notation.
Contd..
WHAT IS CLASS?
One of the principles of software
development is the DRY principle, which
stands for DON’T REPEAT YOURSELF. This
principle is geared towards limiting
repetition within code, and object-oriented
programming adheres to the DRY principle
as it reduces redundancy.
DRY PRINCIPLE
WHAT IS AN OBJECT?
Object is simply a collection of data
(variables) and methods (functions) that act
on those data.
OR
A unique instance of a data structure
that's defined by its class. An object
comprises both data members (class
variables and instance variables) and
methods.
Contd…
WHAT IS AN OBJECT?
OR
An instance of a class. This is the
realized version of the class, where the class
is manifested in the program.
NOTE:
In python everything is an object.
CLASS EXAMPLE
A Python class starts with the reserved
word 'class', followed by the class name and
a colon(:). The simplest form of class
definition looks like this:
class Test:
var=50
marks=10
def display(self):
print (self.var,self.marks)
DEFINING CLASS
DEFINING CLASS
DEFINING AN OBJECT AND FUNCTION CALL
After the class creation one can create
an object. The syntax is,
objectname=classname()
t1=Test()
t1.display()
OBJECT DEFINING
Calling member
function using object
CLASS VARIABLES or ATTRIBUTES or
DATA MEMBERS
A variable that is shared by all instances
of a class. Class variables are defined within a
class and are called ATTRIBUTES in python. In
other languages it is called as DATA MEMBER.
METHODS OR MEMBER FUNCTIONS
A Python method is like a Python function,
but it must be called on an object. And to create
it, you must put it inside a class
In other languages it is called as MEMBER
FUNCTION
Contd..
Python method is like a function, except it is
attached to an object. OR
An function defined in a class is called a
"method". Methods have access to all the data
contained on the instance of the object
METHODS OR MEMBER FUNCTIONS
METHOD DEFINING
Python method is a label that you can call
on an object; it is a piece of code to
execute on that object.
Contd..
self PARAMETER
self PARAMETER
self represents the instance of the class.
By using the "self" keyword we can access the
attributes and methods of the class in python.
NOTE:
self is not keyword. You can
use it as an identifier.
There is not strict rule that you have to
use self. It is the convention, one can use any
thing for example
Instead of
self me is used.
One can use
thisob or any
name.
YOU CAN USE ANYTHING INSTEAD OF self
DIFFERENCE BETWEEN METHODS AND
FUNCTIONS
There is a thin line between the functions
and the methods
The only major difference is that we
call Python method on an object, but it’s not
the same with functions. Also, methods may
modify an object; Python functions don’t.
pass STATEMENT
pass STATEMENT
class Test:
pass
Python pass statement is used when a
statement is required syntactically but you
do not want any command or code to
execute. The pass statement is a null
operation; nothing happens when it
executes. For example:
pass STATEMENT
class Test:
pass
display(self)
print(‘I am method’)
A Class can have method after pass
statement. For example:
del ATTRIBUTE
del ATTRIBUTE
The del() method is used to delete the
named attribute from the object, with the
prior permission of the object.
Syntax is
del objectname, attributename
For example
del ob1,name
Contd… in next slide
del ATTRIBUTE – EXAMPLE (CODE)
del ATTRIBUTE - EXAMPLE (O/P)
SCOPE OF VARIABLES IN CLASS
SCOPE OF VARIABLES IN CLASS
Scope mean measure of access. It is the life
time of the variable.
In the class the scope of variable is
classified into 3 categories
1. Public
2. Private
3. Protected
SCOPE OF VARIABLES IN CLASS
1. Public
By default in class all attributes and
methods are public.
Public members (generally methods
declared in a class) are accessible from
outside the class. The object of the same
class is required to invoke a public method.
The public members are accessed with
dot (.) operator
1. Public
Name and salary
are the attributes
by default public
Object also
accessing the
attribute salary
2. Private
By declaring your data member
private you mean, that nobody should be
able to access it from outside the class, i.e.
strong you can’t touch this policy. Python
supports a technique called name mangling.
Salary is private
member not
accessible by
object or outside
2. Private
Salary is private member
not accessible by object
or outside so attribute
error
2. Private
Python performs name mangling of
private variables. Every member with double
underscore will be changed to
_object._class__variable. If so required, it
can still be accessed from outside the class,
but the practice should be refrained. (Refrain
means stop oneself from doing something)
For example:
Contd..
2. Private
Now salary is made
accessible
2. Private
Output now private member
made accessible, though it is
private variable of class
3. Protected
Protected member is (in C++ and Java)
accessible only from within the class and it’s
subclasses.
How to accomplish this in Python?
The answer is – by convention. By prefixing the
name of your member with a single underscore.
For Example:
Contd..
3. Protected
Making protected variable by
introducing single underscore
3. Protected
Now salary is made
accessible. It is in
protected mode.
SOLVIING CBSE QUESTION PAPER 2(C) 4 Marks
PROGRAMS ON CLASSES
CBSE QUESTION PAPER - 2015 DELHI
(c) Write a class PICTURE in Python with following
specifications : 4
Instance Attributes
– Pno # Numeric value
– Category # String value
– Location # Exhibition Location with
# String value
Methods :
– FixLocation() # A method to assign
# Exhibition Location as per
#Category
# as shown in the following table
CBSE QUESTION PAPER - 2015 DELHI
– Enter() # A function to allow user to
enter values
# Pno, Category and call
FixLocation() method
– SeeAll() # A function to display all the
data members
Category Location
Classic Amina
Modern Jim Plaq
Antique Ustad Khan
CBSE QUESTION PAPER - 2015 DELHI
CBSE QUESTION PAPER - 2015 OUTSIDE DELHI
(c) Write a class PHOTO in Python with following
specifications : 4
Instance Attributes
– Pno # Numeric value
– Category # String Value
– Exhibit # Exhibition Gallery with String value
Methods:
– FixExhibit() #A method to assign
#Exhibition Gallery as per
#Category
#as shown in the following table
CBSE QUESTION PAPER - 2015 OUTSIDE DELHI
Category Exhibit
Antique Zaveri
Modern Johnsen
Classic Terenida
-Register() #A function to allow user to
enter values #Pno, Category
and call FixExhibit() method
– ViewAll() #A function to display all the
data members
CBSE QUESTION PAPER - 2015 OUTSIDE DELHI
CBSE QUESTION PAPER - 2016 DELHI
CBSE QUESTION PAPER - 2016 DELHI
(c) Write a class CITY in Python with following
specifications : 4
Instance Attributes
– Code # Numeric value
– Name # String value
– Pop # Numeric value for Population
– KM # Numeric value
– Density # Numeric value for Population
Density than 12000.
CBSE QUESTION PAPER - 2016 DELHI
Methods:
– CalDen() # Method to calculate Density
as Pop/KM
– Record() # Method to allow user to
enter values Code, Name, Pop,
KM and call CalDen() method
– See() # Method to display all the
data members also display a
message “Highly Populated
Area” if the Density is more
than 12000.
CLASS TEST
1. Write a program to illustrate class.
2.What is object? How it is created explain in
detail with suitable examples.
3.What is visibility mode? What are its types?
Explain in detail the various types of visibility
modes.
4. Explain in detail LEGB Rule
Class : XII Time: 40 Min
Topic: Classes & Objects Max Marks: 40
Each Question carries 5 Marks
Thank You

More Related Content

What's hot

Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
Srinivas Narasegouda
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
Anwar Hasan Shuvo
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
kunal kishore
 
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 

What's hot (20)

Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
OOP java
OOP javaOOP java
OOP java
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 

Similar to Chapter 05 classes and objects

Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
Sudip Simkhada
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
ismartshanker1
 
Oops
OopsOops
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
rani marri
 
Object oriented design
Object oriented designObject oriented design
Object oriented designlykado0dles
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Epic Python Face-Off -Methods vs. Functions.pdf
Epic Python Face-Off -Methods vs. Functions.pdfEpic Python Face-Off -Methods vs. Functions.pdf
Epic Python Face-Off -Methods vs. Functions.pdf
SudhanshiBakre1
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objectskhaliledapal
 
Epic Python Face-Off -Methods vs.pdf
Epic Python Face-Off -Methods vs.pdfEpic Python Face-Off -Methods vs.pdf
Epic Python Face-Off -Methods vs.pdf
SudhanshiBakre1
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
rumanatasnim415
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
MLG College of Learning, Inc
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Class and object
Class and objectClass and object
Class and object
Prof. Dr. K. Adisesha
 

Similar to Chapter 05 classes and objects (20)

My c++
My c++My c++
My c++
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
Oops
OopsOops
Oops
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
Object oriented design
Object oriented designObject oriented design
Object oriented design
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Epic Python Face-Off -Methods vs. Functions.pdf
Epic Python Face-Off -Methods vs. Functions.pdfEpic Python Face-Off -Methods vs. Functions.pdf
Epic Python Face-Off -Methods vs. Functions.pdf
 
Oops
OopsOops
Oops
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 
Epic Python Face-Off -Methods vs.pdf
Epic Python Face-Off -Methods vs.pdfEpic Python Face-Off -Methods vs.pdf
Epic Python Face-Off -Methods vs.pdf
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Class and object
Class and objectClass and object
Class and object
 

More from Praveen M Jigajinni

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
Praveen M Jigajinni
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
Praveen M Jigajinni
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
Praveen M Jigajinni
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
Praveen M Jigajinni
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
Praveen M Jigajinni
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
Praveen M Jigajinni
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
Praveen M Jigajinni
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
Praveen M Jigajinni
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
Praveen M Jigajinni
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
Praveen M Jigajinni
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
Praveen M Jigajinni
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
Praveen M Jigajinni
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
Praveen M Jigajinni
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
Praveen M Jigajinni
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
Praveen M Jigajinni
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computing
Praveen M Jigajinni
 
Chapter 2 operating systems
Chapter 2 operating systemsChapter 2 operating systems
Chapter 2 operating systems
Praveen M Jigajinni
 

More from Praveen M Jigajinni (20)

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computing
 
Chapter 2 operating systems
Chapter 2 operating systemsChapter 2 operating systems
Chapter 2 operating systems
 

Recently uploaded

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

Chapter 05 classes and objects

  • 1. CHAPTER - 05 CLASSES AND OBJECTS
  • 2. Unit I Programming and Computational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
  • 4. INTRODUCTION This chapter deals with classes in Python. As Python is fully object-oriented, you can define your own Classes, inherit from your own or built-in classes, and instantiate the classes that you've defined. But before we start with our study on classes let us understand about namespaces and scope rules in Python.
  • 6. These consist of functions such as max() , min() and built-in exception names. This namespace is created when the Python interpreter starts up, and is never deleted. The built-in names actually also live in a module called__ builtin__ 1. BULIT IN NAMESPACE
  • 7. The global namespace for a module is created when the module definition is read in and normally lasts until the interpreter quits. The statements executed by the top-level invocation of the interpreter, either read from a script file or interactively, are considered to be part of a module called__main__ and they have their own global namespace. 2. GLOBAL NAMES IN A MODULE
  • 8. The global namespace for a module is created when the module definition is read in and normally lasts until the interpreter quits. The statements executed by the top-level invocation of the interpreter, either read from a script file or interactively, are considered to be part of a module called__main__ and they have their own global namespace. 3. LOCAL NAMES IN A FUNCTION INVOCATION
  • 9. The local namespace for a function is created when the function is called, and deleted when the function returns or raises an exception that is not handled within the function. Even each recursive invocation has its own local namespace. 3. LOCAL NAMES IN A FUNCTION INVOCATION
  • 10. The local namespace for a function is created when the function is called, and deleted when the function returns or raises an exception that is not handled within the function. Even each recursive invocation has its own local namespace. If we talk about classes and objects, the set of attributes of an object also form a namespace. contd… 3. LOCAL NAMES IN A FUNCTION INVOCATION
  • 11. It must be noted that there is absolutely no relation between names in different namespaces. Two different modules may both define same function without any confusion because the functions are prefixed with the module name. That means module1.cmp() has no relation with module2.cmp(). 3. LOCAL NAMES IN A FUNCTION INVOCATION
  • 13. A scope is a region of a Python program where a namespace is directly accessible. The location where the names are assigned in the code determines the scope of visibility of the name in that code. SCOPE RULES
  • 15. According to this rule, when a name is encountered during the execution of the program, it searches for that name in the following order: L. Local E. Enclosing functions G. Global (module) B. Built-in (Python) LEGB RULE
  • 16. L. Local - It first makes a local search i.e. in current def statement. The import statements and function definitions bind the module or function name in the local scope. In fact, all operations that introduce new names use the local scope. LEGB RULE – L Local
  • 17. E. Enclosing functions - It searches in all enclosing functions, form inner to outer. LEGB RULE – E. Enclosing functions
  • 18. G. Global (module) - It searches for global modules or for names declared global in a def within the file. LEGB RULE – G. Global (module)
  • 19. B. Built-in (Python) - Finally it checks for any built in functions in Python. LEGB RULE – B. Built-in (Python)
  • 20. CLASSES AND OBJECTS DEFINING CLASSES AND OBJECTS
  • 21. Python is an object-oriented programming language. Object-oriented programming (OOP) focuses on creating reusable patterns of code. A blueprint or prototypes created by a programmer for an object is called class. Class is a container of attributes and methods. Its an Abstract Data Type (ADT) Contd… WHAT IS CLASS?
  • 22. A USER-DEFINED PROTOTYPE for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. Contd.. WHAT IS CLASS?
  • 23. One of the principles of software development is the DRY principle, which stands for DON’T REPEAT YOURSELF. This principle is geared towards limiting repetition within code, and object-oriented programming adheres to the DRY principle as it reduces redundancy. DRY PRINCIPLE
  • 24. WHAT IS AN OBJECT? Object is simply a collection of data (variables) and methods (functions) that act on those data. OR A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods. Contd…
  • 25. WHAT IS AN OBJECT? OR An instance of a class. This is the realized version of the class, where the class is manifested in the program. NOTE: In python everything is an object.
  • 27. A Python class starts with the reserved word 'class', followed by the class name and a colon(:). The simplest form of class definition looks like this: class Test: var=50 marks=10 def display(self): print (self.var,self.marks) DEFINING CLASS
  • 29. DEFINING AN OBJECT AND FUNCTION CALL After the class creation one can create an object. The syntax is, objectname=classname() t1=Test() t1.display() OBJECT DEFINING Calling member function using object
  • 30. CLASS VARIABLES or ATTRIBUTES or DATA MEMBERS A variable that is shared by all instances of a class. Class variables are defined within a class and are called ATTRIBUTES in python. In other languages it is called as DATA MEMBER.
  • 31. METHODS OR MEMBER FUNCTIONS A Python method is like a Python function, but it must be called on an object. And to create it, you must put it inside a class In other languages it is called as MEMBER FUNCTION Contd.. Python method is like a function, except it is attached to an object. OR An function defined in a class is called a "method". Methods have access to all the data contained on the instance of the object
  • 32. METHODS OR MEMBER FUNCTIONS METHOD DEFINING Python method is a label that you can call on an object; it is a piece of code to execute on that object. Contd..
  • 34. self PARAMETER self represents the instance of the class. By using the "self" keyword we can access the attributes and methods of the class in python. NOTE: self is not keyword. You can use it as an identifier.
  • 35. There is not strict rule that you have to use self. It is the convention, one can use any thing for example Instead of self me is used. One can use thisob or any name. YOU CAN USE ANYTHING INSTEAD OF self
  • 36. DIFFERENCE BETWEEN METHODS AND FUNCTIONS There is a thin line between the functions and the methods The only major difference is that we call Python method on an object, but it’s not the same with functions. Also, methods may modify an object; Python functions don’t.
  • 38. pass STATEMENT class Test: pass Python pass statement is used when a statement is required syntactically but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes. For example:
  • 39. pass STATEMENT class Test: pass display(self) print(‘I am method’) A Class can have method after pass statement. For example:
  • 41. del ATTRIBUTE The del() method is used to delete the named attribute from the object, with the prior permission of the object. Syntax is del objectname, attributename For example del ob1,name Contd… in next slide
  • 42. del ATTRIBUTE – EXAMPLE (CODE)
  • 43. del ATTRIBUTE - EXAMPLE (O/P)
  • 44. SCOPE OF VARIABLES IN CLASS
  • 45. SCOPE OF VARIABLES IN CLASS Scope mean measure of access. It is the life time of the variable. In the class the scope of variable is classified into 3 categories 1. Public 2. Private 3. Protected
  • 46. SCOPE OF VARIABLES IN CLASS 1. Public By default in class all attributes and methods are public. Public members (generally methods declared in a class) are accessible from outside the class. The object of the same class is required to invoke a public method. The public members are accessed with dot (.) operator
  • 47. 1. Public Name and salary are the attributes by default public Object also accessing the attribute salary
  • 48. 2. Private By declaring your data member private you mean, that nobody should be able to access it from outside the class, i.e. strong you can’t touch this policy. Python supports a technique called name mangling. Salary is private member not accessible by object or outside
  • 49. 2. Private Salary is private member not accessible by object or outside so attribute error
  • 50. 2. Private Python performs name mangling of private variables. Every member with double underscore will be changed to _object._class__variable. If so required, it can still be accessed from outside the class, but the practice should be refrained. (Refrain means stop oneself from doing something) For example: Contd..
  • 51. 2. Private Now salary is made accessible
  • 52. 2. Private Output now private member made accessible, though it is private variable of class
  • 53. 3. Protected Protected member is (in C++ and Java) accessible only from within the class and it’s subclasses. How to accomplish this in Python? The answer is – by convention. By prefixing the name of your member with a single underscore. For Example: Contd..
  • 54. 3. Protected Making protected variable by introducing single underscore
  • 55. 3. Protected Now salary is made accessible. It is in protected mode.
  • 56. SOLVIING CBSE QUESTION PAPER 2(C) 4 Marks PROGRAMS ON CLASSES
  • 57. CBSE QUESTION PAPER - 2015 DELHI
  • 58. (c) Write a class PICTURE in Python with following specifications : 4 Instance Attributes – Pno # Numeric value – Category # String value – Location # Exhibition Location with # String value Methods : – FixLocation() # A method to assign # Exhibition Location as per #Category # as shown in the following table CBSE QUESTION PAPER - 2015 DELHI
  • 59. – Enter() # A function to allow user to enter values # Pno, Category and call FixLocation() method – SeeAll() # A function to display all the data members Category Location Classic Amina Modern Jim Plaq Antique Ustad Khan CBSE QUESTION PAPER - 2015 DELHI
  • 60. CBSE QUESTION PAPER - 2015 OUTSIDE DELHI
  • 61. (c) Write a class PHOTO in Python with following specifications : 4 Instance Attributes – Pno # Numeric value – Category # String Value – Exhibit # Exhibition Gallery with String value Methods: – FixExhibit() #A method to assign #Exhibition Gallery as per #Category #as shown in the following table CBSE QUESTION PAPER - 2015 OUTSIDE DELHI
  • 62. Category Exhibit Antique Zaveri Modern Johnsen Classic Terenida -Register() #A function to allow user to enter values #Pno, Category and call FixExhibit() method – ViewAll() #A function to display all the data members CBSE QUESTION PAPER - 2015 OUTSIDE DELHI
  • 63. CBSE QUESTION PAPER - 2016 DELHI
  • 64. CBSE QUESTION PAPER - 2016 DELHI (c) Write a class CITY in Python with following specifications : 4 Instance Attributes – Code # Numeric value – Name # String value – Pop # Numeric value for Population – KM # Numeric value – Density # Numeric value for Population Density than 12000.
  • 65. CBSE QUESTION PAPER - 2016 DELHI Methods: – CalDen() # Method to calculate Density as Pop/KM – Record() # Method to allow user to enter values Code, Name, Pop, KM and call CalDen() method – See() # Method to display all the data members also display a message “Highly Populated Area” if the Density is more than 12000.
  • 66. CLASS TEST 1. Write a program to illustrate class. 2.What is object? How it is created explain in detail with suitable examples. 3.What is visibility mode? What are its types? Explain in detail the various types of visibility modes. 4. Explain in detail LEGB Rule Class : XII Time: 40 Min Topic: Classes & Objects Max Marks: 40 Each Question carries 5 Marks

Editor's Notes

  1. u
  2. u
  3. u
  4. u
  5. u
  6. u
  7. u
  8. u
  9. u
  10. u
  11. u