SlideShare a Scribd company logo
http://www.skillbrew.com
/Skillbrew
Talent brewed by the
industry itself
Classes and Objects
Pavan Verma
@YinYangPavan
Founder, P3 InfoTech Solutions Pvt. Ltd.
1
Python Programming Essentials
© SkillBrew http://skillbrew.com
Contents
 Defining a class
 Class attributes
 Class methods
 Class instances
 __init__ method
 self keyword
 Accessing attributes and methods
 Deleting attributes
 Types of attributes
 Inheritance
 Method overriding
 Calling parent functions
2
© SkillBrew http://skillbrew.com
Defining a class
A class is a special data type which defines how
to build a certain kind of object
class className():
# statements
Use the class keyword to define a class
3
© SkillBrew http://skillbrew.com
Defining a class
class Calculator():
counter = 0
def __init__(self):
pass
def add(self):
pass
class keyword to
define a class
A class definition creates a class object from which
class instances may be created
4
© SkillBrew http://skillbrew.com
Class Attributes
class Calculator():
counter = 0
def __init__(self):
pass
def add(self):
pass
class attributes are
just like variables
5
© SkillBrew http://skillbrew.com
Class Methods
class Calculator():
counter = 0
def __init__(self):
pass
def add(self):
pass
class methods are
functions invoked on an
instance of the class
6
© SkillBrew http://skillbrew.com
Class Instances
calc = Calculator()
• In order to you use it we create an instance of
class
• Instances are objects created that use the class
definition
Just call the class definition like a function to create
a class instance
7
© SkillBrew http://skillbrew.com
__init__ method
• __init__ method is like an initialization
constructor
• When a class defines an __init__ ()
method, class instantiation automatically invokes
__init__() method for the newly created
class instance
8
© SkillBrew http://skillbrew.com
__init__ method (2)
class Calculator():
counter = 0
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def add(self):
pass
calc = Calculator(10, 20)
print calc.x
print calc.y
Output:
10
20
9
© SkillBrew http://skillbrew.com
self keyword
• The first argument of every method is a reference
to the current instance of the class
• By convention, we name this argument self
• In __init__, self refers to the object currently
being created
• In other class methods, it refers to the instance
whose method was called
• Similar to the keyword this in Java or C++
10
© SkillBrew http://skillbrew.com
Accessing attributes and methods
Use the dot operator to access class attributes and
methods
calc = Calculator(10, 20)
print calc.x
print calc.y
print calc.counter
Output:
10
20
0
11
© SkillBrew http://skillbrew.com
Accessing attributes and methods (2)
class Calculator():
counter = 0
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def add(self):
return self.x + self.y
calc = Calculator(10, 20)
print calc.add()
• Although you must specify self
explicitly when defining the
method, you don’t include it
when calling the method
• Python passes it for you
automatically
12
© SkillBrew http://skillbrew.com
Deleting Instances
• When you are done with an object , you don’t have
to delete or free it explicitly
• Python has automatic garbage collection
• Python will automatically detect when all references
to a piece of memory have gone out of scope.
Automatically frees the memory.
• Garbage collection works well, hence fewer memory
leaks
• There’s also no “destructor” method for classes.
13
© SkillBrew http://skillbrew.com
Attributes
14
© SkillBrew http://skillbrew.com
Two kinds of Attributes
1. class attributes
2. data attributes
15
© SkillBrew http://skillbrew.com
Data attributes
class Calculator():
counter = 0
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def add(self):
return self.x + self.y
calc = Calculator(10, 20)
print calc.x # 10
print calc.y # 20
calc2 = Calculator(15, 35)
print calc2.x # 15
print calc2.y # 35
• Data attributes are
variables owned by a
particular instance
• Each instance has its own
value for data attributes
16
© SkillBrew http://skillbrew.com
Data attributes (2)
class Calculator():
counter = 0
def __init__(self, x=0, y=0):
self.x = x
self.y = y
calc = Calculator(10, 20)
calc.z = calc.x + calc.y
print calc.z
print calc.__dict__
Output:
30
{'y': 20, 'x': 10, 'z': 30}
17
• In Python classes you don’t
have a restriction of
declaring all data
attributes before hand,
you can create data
attributes at runtime
anywhere
• calc.z is an attribute
which is defined at
runtime outside the class
definition
© SkillBrew http://skillbrew.com
Class attributes
class Calculator():
counter = 0
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def add(self):
self.__class__.counter += 1
return self.x + self.y
calc = Calculator(10, 20)
print calc.add()
print calc.counter # 1
calc2 = Calculator(30, 40)
print calc2.add()
print calc2.counter # 2
• Access the class
attribute using
self.__class__.count
er
• Class attributes are
shared among all
instances
18
© SkillBrew http://skillbrew.com
Class attributes (2)
 Class attributes are defined within a class definition
and outside of any method
 Because all instances of a class share one copy of a
class attribute, when any instance changes it, the
value is changed for all instances
self.__class__.attribute_name
19
© SkillBrew http://skillbrew.com
Data attributes
 Variable owned by a
particular instance
 Each instance has its own
value for it
 These are the most
common kind of attribute
Class attributes
 Owned by the class as a
whole
 All class instances share the
same value for it
 Good for
• Class-wide constants
• Building counter of how
many instances of the
class have been made
Data attributes vs Class attributes
20
© SkillBrew http://skillbrew.com
Inheritance
21
© SkillBrew http://skillbrew.com
Inheritance
class Shape(object):
def name(self, shape):
print "Shape: %s" % shape
class Square(Shape):
def area(self, side):
return side**2
Shape is the
parent class
Square is the
child class inherits
Shape
class Parent(object):
pass
class Child(Parent):
pass
22
© SkillBrew http://skillbrew.com
Inheritance (2)
class Shape(object):
def name(self, shape):
print "Shape: %s" % shape
class Square(Shape):
def area(self, side):
return side**2
s = Square()
s.name("square")
print s.area(2)
Output:
Shape: square
4
Child class Square has access to
Parent classes methods and
attributes
23
© SkillBrew http://skillbrew.com
Method overriding
class Shape(object):
def name(self, shape):
print "Shape: %s" % shape
class Square(Shape):
def name(self, shape):
print "Child class Shape %s" % shape
def area(self, side):
return side**2
s = Square()
s.name("square")
print s.area(2)
Output:
Child class Shape square
4
24
© SkillBrew http://skillbrew.com
Calling the parent method
class Shape(object):
def name(self, shape):
print "Shape: %s" % shape
class Square(Shape):
def name(self, shape):
super(Square, self).name(shape)
print "Child class Shape %s" % shape
def area(self, side):
return side**2
s = Square()
s.name("square")
Use super keyword to call parent class method
super(ChildClass, self).method(args)
25
© SkillBrew http://skillbrew.com
Class & Static Methods
class Calculator(object):
counter = 0
def __init__(self, x=0, y=0):
...
def add(self):
...
@classmethod
def update_counter(cls):
cls.counter += 1
@staticmethod
def show_help():
print 'This calculator can
add'
26
© SkillBrew http://skillbrew.com
Resources
 http://www.diveintopython.net/object_o
riented_framework/defining_classes.html
 http://docs.python.org/2/tutorial/classes
.html
 http://docs.python.org/2/library/function
s.html#super
27

More Related Content

PPTX
Activation function
Astha Jain
 
PPT
Packages in java
jamunaashok
 
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
PPTX
Object oriented programming with python
Arslan Arshad
 
PPTX
Inheritance in java
Tech_MX
 
PDF
Python set
Mohammed Sikander
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPTX
trigger dbms
kuldeep100
 
Activation function
Astha Jain
 
Packages in java
jamunaashok
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Object oriented programming with python
Arslan Arshad
 
Inheritance in java
Tech_MX
 
Python set
Mohammed Sikander
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
trigger dbms
kuldeep100
 

What's hot (20)

PPTX
Class, object and inheritance in python
Santosh Verma
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PPTX
Unsupervised learning clustering
Arshad Farhad
 
PPTX
Group By, Order By, and Aliases in SQL
MSB Academy
 
PDF
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
PPTX
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
PPTX
Packages in PL/SQL
Pooja Dixit
 
PDF
Python - object oriented
Learnbay Datascience
 
PPTX
DBSCAN (1) (4).pptx
ABINPMATHEW22020
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
PDF
Performance Metrics for Machine Learning Algorithms
Kush Kulshrestha
 
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
PDF
CART: Not only Classification and Regression Trees
Marc Garcia
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPTX
Object Oriented Programming in Python
Sujith Kumar
 
PDF
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
PPTX
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
PPTX
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
PPT
Arrays in php
Laiby Thomas
 
Class, object and inheritance in python
Santosh Verma
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Unsupervised learning clustering
Arshad Farhad
 
Group By, Order By, and Aliases in SQL
MSB Academy
 
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Packages in PL/SQL
Pooja Dixit
 
Python - object oriented
Learnbay Datascience
 
DBSCAN (1) (4).pptx
ABINPMATHEW22020
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Performance Metrics for Machine Learning Algorithms
Kush Kulshrestha
 
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
CART: Not only Classification and Regression Trees
Marc Garcia
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Object Oriented Programming in Python
Sujith Kumar
 
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
Arrays in php
Laiby Thomas
 
Ad

Viewers also liked (20)

PPT
Python Objects
Quintagroup
 
PDF
Object Oriented Programming with Real World Examples
OXUS 20
 
ODP
Decorators in Python
Ben James
 
PDF
Python Programming - VI. Classes and Objects
Ranel Padon
 
PPTX
Object oriented programming Fundamental Concepts
Bharat Kalia
 
PPTX
Creating Objects in Python
Damian T. Gordon
 
PPTX
03 object-classes-pbl-4-slots
mha4
 
PPTX
Classes & object
Daman Toor
 
PDF
PythonOOP
Veera Pendyala
 
PDF
Metaclass Programming in Python
Juan-Manuel Gimeno
 
PPTX
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
P3 InfoTech Solutions Pvt. Ltd.
 
PDF
Introduction To Programming with Python
Sushant Mane
 
PDF
Boost your django admin with Grappelli
Andy Dai
 
PDF
Visualizing Relationships between Python objects - EuroPython 2008
Dinu Gherman
 
PDF
HT16 - DA361A - OOP med Python
Anton Tibblin
 
PDF
Python avancé : Classe et objet
ECAM Brussels Engineering School
 
PDF
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
PPTX
Python Programming Essentials - M8 - String Methods
P3 InfoTech Solutions Pvt. Ltd.
 
PDF
CLTL python course: Object Oriented Programming (1/3)
Rubén Izquierdo Beviá
 
PPTX
Python Programming Essentials - M1 - Course Introduction
P3 InfoTech Solutions Pvt. Ltd.
 
Python Objects
Quintagroup
 
Object Oriented Programming with Real World Examples
OXUS 20
 
Decorators in Python
Ben James
 
Python Programming - VI. Classes and Objects
Ranel Padon
 
Object oriented programming Fundamental Concepts
Bharat Kalia
 
Creating Objects in Python
Damian T. Gordon
 
03 object-classes-pbl-4-slots
mha4
 
Classes & object
Daman Toor
 
PythonOOP
Veera Pendyala
 
Metaclass Programming in Python
Juan-Manuel Gimeno
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
P3 InfoTech Solutions Pvt. Ltd.
 
Introduction To Programming with Python
Sushant Mane
 
Boost your django admin with Grappelli
Andy Dai
 
Visualizing Relationships between Python objects - EuroPython 2008
Dinu Gherman
 
HT16 - DA361A - OOP med Python
Anton Tibblin
 
Python avancé : Classe et objet
ECAM Brussels Engineering School
 
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Python Programming Essentials - M8 - String Methods
P3 InfoTech Solutions Pvt. Ltd.
 
CLTL python course: Object Oriented Programming (1/3)
Rubén Izquierdo Beviá
 
Python Programming Essentials - M1 - Course Introduction
P3 InfoTech Solutions Pvt. Ltd.
 
Ad

Similar to Python Programming Essentials - M20 - Classes and Objects (20)

PDF
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
PDF
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
PPTX
Object Oriented Programming.pptx
SAICHARANREDDYN
 
PPTX
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PPT
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
PPT
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
PPTX
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
PPT
Python3
Ruchika Sinha
 
PPT
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
 
PPTX
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
PPTX
VTU Python Module 5 , Class and Objects and Debugging
rickyghoshiit
 
PPTX
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
PPTX
classes and objects of python object oriented
VineelaThonduri
 
PPTX
Python advance
Mukul Kirti Verma
 
PPTX
Python OOPs
Binay Kumar Ray
 
PDF
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
Introduction to Python - Part Three
amiable_indian
 
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Python3
Ruchika Sinha
 
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
 
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
VTU Python Module 5 , Class and Objects and Debugging
rickyghoshiit
 
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
classes and objects of python object oriented
VineelaThonduri
 
Python advance
Mukul Kirti Verma
 
Python OOPs
Binay Kumar Ray
 
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 

More from P3 InfoTech Solutions Pvt. Ltd. (20)

PPTX
Python Programming Essentials - M44 - Overview of Web Development
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M40 - Invoking External Programs
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M39 - Unit Testing
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M35 - Iterators & Generators
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M34 - List Comprehensions
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M31 - PEP 8
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M29 - Python Interpreter and Files
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M25 - os and sys modules
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M24 - math module
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M22 - File Operations
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M18 - Modules and Packages
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M17 - Functions
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M15 - References
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M14 - Dictionaries
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M44 - Overview of Web Development
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M40 - Invoking External Programs
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M39 - Unit Testing
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M35 - Iterators & Generators
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M34 - List Comprehensions
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M31 - PEP 8
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M29 - Python Interpreter and Files
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M25 - os and sys modules
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M24 - math module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M22 - File Operations
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M18 - Modules and Packages
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M17 - Functions
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M15 - References
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M14 - Dictionaries
P3 InfoTech Solutions Pvt. Ltd.
 

Python Programming Essentials - M20 - Classes and Objects