SlideShare a Scribd company logo
Python Classes/Objects

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.
Class

A variable that is shared by all instances of a class.
Class variables are defined within a class but outside
any of the class's methods.

Class variables aren't used as frequently as instance
variables are.
Class variable

The class statement creates a new class definition.

The name of the class immediately follows the
keyword class followed by a colon as follows:

Class ClassName:

‘Optional class documentation string‘

class_suite
Creating Classes

The class has a documentation string, which can be
accessed via ClassName.__doc__.

The class_suite consists of all the component
statements defining class members, data attributes and
functions.
Class Employee:
'Common base class for all employees'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1

The first method __init__() is a special method, which
is called class constructor or initialization method that
Python calls when you create a new instance of this
class.

def displayCount(self):

print "Total Employee %d" Employee.empCount

def displayEmployee(self):

print "Name : ", self.name, ", Salary: ", self.salary
"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2
Output

Create a class by deriving it from a preexisting class

by listing the parent class in parentheses after the
new class name.

The child class inherits the attributes of its parent
class, and you can use those attributes as if they
were defined in the child class. A child class can
also override data members and methods
from the parent.
Class Inheritance
class Parent: # define parent class
parentAttr = 100
def __init__(self):
print "Calling parent constructor"
def parentMethod(self):
print 'Calling parent method'
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print "Parent attribute :", Parent.parentAttr
class Child(Parent): # define child class
def __init__(self):
print "Calling child constructor"
def childMethod(self):
print 'Calling child method'
c = Child() # instance of child
c.childMethod() # child calls its method
c.parentMethod() # calls parent's method
c.setAttr(200) # again call parent's method
c.GetAttr() # again call parent's method

Output:
Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200
Python session 7 by Shan

More Related Content

PPTX
Python Classes and Objects part13
PPT
Spsl v unit - final
PPTX
Inheritance in java
PDF
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PDF
Python programming : Inheritance and polymorphism
PDF
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
PDF
Python programming : Classes objects
PDF
Python unit 3 m.sc cs
Python Classes and Objects part13
Spsl v unit - final
Inheritance in java
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Python programming : Inheritance and polymorphism
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Python programming : Classes objects
Python unit 3 m.sc cs

What's hot (20)

PPTX
Advanced Python : Static and Class Methods
PPT
Java class
PDF
Java keywords
PDF
Java Basic day-2
DOCX
Python Metaclasses
PPTX
Inheritance in java
PPS
Introduction to class in java
PDF
ITFT-Classes and object in java
PPTX
Python advance
PPTX
Lightning talk
PPTX
Inheritance in Java
PPTX
Python oop third class
PDF
Using and Abusing Magic methods in Python
PPTX
Python: Basic Inheritance
DOC
Advanced core java
PDF
Python Class | Python Programming | Python Tutorial | Edureka
PDF
Python - object oriented
PDF
Lect 1-java object-classes
PPT
Class and object in c++
Advanced Python : Static and Class Methods
Java class
Java keywords
Java Basic day-2
Python Metaclasses
Inheritance in java
Introduction to class in java
ITFT-Classes and object in java
Python advance
Lightning talk
Inheritance in Java
Python oop third class
Using and Abusing Magic methods in Python
Python: Basic Inheritance
Advanced core java
Python Class | Python Programming | Python Tutorial | Edureka
Python - object oriented
Lect 1-java object-classes
Class and object in c++
Ad

Viewers also liked (6)

PPT
PPTX
Tuples, Dicts and Exception Handling
PDF
Python Programming - X. Exception Handling and Assertions
PPT
Python advanced 1.handle error, generator, decorator and decriptor
PPTX
Python advanced 2. regular expression in python
PDF
Python exceptions
Tuples, Dicts and Exception Handling
Python Programming - X. Exception Handling and Assertions
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 2. regular expression in python
Python exceptions
Ad

Similar to Python session 7 by Shan (20)

PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPTX
Presentation_4516_Content_Document_20250204010703PM.pptx
PDF
Object_Oriented_Programming_Unit3.pdf
PPTX
IPP-M5-C1-Classes _ Objects python -S2.pptx
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PPTX
Python_Object_Oriented_Programming.pptx
PPTX
Object Oriented Programming.pptx
PPTX
Python Lecture 13
PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PPTX
My Object Oriented.pptx
PDF
Object Oriented programming Using Python.pdf
PDF
9-_Object_Oriented_Programming_Using_Python.pdf
PPT
Python3
PPT
Lecture topic - Python class lecture.ppt
PPT
Lecture on Python class -lecture123456.ppt
PPTX
9-_Object_Oriented_Programming_Using_Python 1.pptx
PPTX
PPTX
ITS-16163: Module 7 Introduction to Object-Oriented Programming Basics
PPTX
Module-5-Classes and Objects for Python Programming.pptx
PPTX
Basic_concepts_of_OOPS_in_Python.pptx
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Presentation_4516_Content_Document_20250204010703PM.pptx
Object_Oriented_Programming_Unit3.pdf
IPP-M5-C1-Classes _ Objects python -S2.pptx
Unit 3-Classes ,Objects and Inheritance.pdf
Python_Object_Oriented_Programming.pptx
Object Oriented Programming.pptx
Python Lecture 13
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
My Object Oriented.pptx
Object Oriented programming Using Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf
Python3
Lecture topic - Python class lecture.ppt
Lecture on Python class -lecture123456.ppt
9-_Object_Oriented_Programming_Using_Python 1.pptx
ITS-16163: Module 7 Introduction to Object-Oriented Programming Basics
Module-5-Classes and Objects for Python Programming.pptx
Basic_concepts_of_OOPS_in_Python.pptx

More from Navaneethan Naveen (20)

PPT
Class inheritance 13 session - SHAN
PPT
Python session 12
PPT
Python session 11
PPT
V irtualisation.1
PPT
Python session.11 By Shanmugam
PPT
Virtualisation-11
PPT
Networking session-4-final by aravind.R
PPT
Networking session3
PPT
WIN-ADCS-10
PPT
Python session 10
PPT
Python multithreading session 9 - shanmugam
PPT
Python session 8
PPT
PPT
Virtualization session 8
PDF
Virtualization session 7 by Gugan
PPT
Virtualization s4.1
PPT
Python session 6
PPT
Gpo windows(4)
PPTX
Windows session 5 : Basics of active directory
PPT
Network session 1 OSI Model
Class inheritance 13 session - SHAN
Python session 12
Python session 11
V irtualisation.1
Python session.11 By Shanmugam
Virtualisation-11
Networking session-4-final by aravind.R
Networking session3
WIN-ADCS-10
Python session 10
Python multithreading session 9 - shanmugam
Python session 8
Virtualization session 8
Virtualization session 7 by Gugan
Virtualization s4.1
Python session 6
Gpo windows(4)
Windows session 5 : Basics of active directory
Network session 1 OSI Model

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Computing-Curriculum for Schools in Ghana
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Classroom Observation Tools for Teachers
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial disease of the cardiovascular and lymphatic systems
A systematic review of self-coping strategies used by university students to ...
VCE English Exam - Section C Student Revision Booklet
Computing-Curriculum for Schools in Ghana
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Final Presentation General Medicine 03-08-2024.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Classroom Observation Tools for Teachers
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Python session 7 by Shan

  • 2.  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. Class
  • 3.  A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods.  Class variables aren't used as frequently as instance variables are. Class variable
  • 4.  The class statement creates a new class definition.  The name of the class immediately follows the keyword class followed by a colon as follows:  Class ClassName:  ‘Optional class documentation string‘  class_suite Creating Classes
  • 5.  The class has a documentation string, which can be accessed via ClassName.__doc__.  The class_suite consists of all the component statements defining class members, data attributes and functions.
  • 6. Class Employee: 'Common base class for all employees' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1
  • 7.  The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.  def displayCount(self):  print "Total Employee %d" Employee.empCount  def displayEmployee(self):  print "Name : ", self.name, ", Salary: ", self.salary
  • 8. "This would create first object of Employee class" emp1 = Employee("Zara", 2000) "This would create second object of Employee class" emp2 = Employee("Manni", 5000) emp1.displayEmployee() emp2.displayEmployee() print "Total Employee %d" % Employee.empCount
  • 9. Name : Zara ,Salary: 2000 Name : Manni ,Salary: 5000 Total Employee 2 Output
  • 10.  Create a class by deriving it from a preexisting class  by listing the parent class in parentheses after the new class name.  The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent. Class Inheritance
  • 11. class Parent: # define parent class parentAttr = 100 def __init__(self): print "Calling parent constructor" def parentMethod(self): print 'Calling parent method' def setAttr(self, attr): Parent.parentAttr = attr
  • 12. def getAttr(self): print "Parent attribute :", Parent.parentAttr class Child(Parent): # define child class def __init__(self): print "Calling child constructor" def childMethod(self): print 'Calling child method' c = Child() # instance of child
  • 13. c.childMethod() # child calls its method c.parentMethod() # calls parent's method c.setAttr(200) # again call parent's method c.GetAttr() # again call parent's method  Output: Calling child constructor Calling child method Calling parent method Parent attribute : 200