SlideShare una empresa de Scribd logo
1 de 14
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Python classes objects
Python classes objectsPython classes objects
Python classes objects
 
Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods
 
Java class
Java classJava class
Java class
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Python Metaclasses
Python MetaclassesPython Metaclasses
Python Metaclasses
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Python advance
Python advancePython advance
Python advance
 
Lightning talk
Lightning talkLightning talk
Lightning talk
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Using and Abusing Magic methods in Python
Using and Abusing Magic methods in PythonUsing and Abusing Magic methods in Python
Using and Abusing Magic methods in Python
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Advanced core java
Advanced core javaAdvanced core java
Advanced core java
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 

Destacado (6)

Exception
ExceptionException
Exception
 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception Handling
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Python exceptions
Python exceptionsPython exceptions
Python exceptions
 

Similar a Python session 7 by Shan

Class inheritance 13 session - SHAN
Class inheritance 13 session - SHANClass inheritance 13 session - SHAN
Class inheritance 13 session - SHAN
Navaneethan Naveen
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov
 

Similar a Python session 7 by Shan (20)

PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
My Object Oriented.pptx
My Object Oriented.pptxMy Object Oriented.pptx
My Object Oriented.pptx
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
Python3
Python3Python3
Python3
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
 
Pythonclass
PythonclassPythonclass
Pythonclass
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfPython Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfPython Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
 
Class inheritance
Class inheritanceClass inheritance
Class inheritance
 
Class inheritance 13 session - SHAN
Class inheritance 13 session - SHANClass inheritance 13 session - SHAN
Class inheritance 13 session - SHAN
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
 

Más de Navaneethan Naveen (20)

Python session 12
Python session 12Python session 12
Python session 12
 
Python session 11
Python session 11Python session 11
Python session 11
 
V irtualisation.1
V irtualisation.1V irtualisation.1
V irtualisation.1
 
Python session.11 By Shanmugam
Python session.11 By ShanmugamPython session.11 By Shanmugam
Python session.11 By Shanmugam
 
Virtualisation-11
Virtualisation-11Virtualisation-11
Virtualisation-11
 
Networking session-4-final by aravind.R
Networking session-4-final by aravind.RNetworking session-4-final by aravind.R
Networking session-4-final by aravind.R
 
Networking session3
Networking session3Networking session3
Networking session3
 
WIN-ADCS-10
WIN-ADCS-10WIN-ADCS-10
WIN-ADCS-10
 
Python session 10
Python session 10Python session 10
Python session 10
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
Python session 8
Python session 8Python session 8
Python session 8
 
Win 8th
Win 8thWin 8th
Win 8th
 
Virtualization session 8
Virtualization session 8Virtualization session 8
Virtualization session 8
 
Virtualization session 7 by Gugan
Virtualization session 7 by GuganVirtualization session 7 by Gugan
Virtualization session 7 by Gugan
 
Virtualization s4.1
Virtualization s4.1Virtualization s4.1
Virtualization s4.1
 
Python session 6
Python session 6Python session 6
Python session 6
 
Gpo windows(4)
Gpo windows(4)Gpo windows(4)
Gpo windows(4)
 
Windows session 5 : Basics of active directory
Windows session 5 : Basics of active directoryWindows session 5 : Basics of active directory
Windows session 5 : Basics of active directory
 
Network session 1 OSI Model
Network session 1 OSI ModelNetwork session 1 OSI Model
Network session 1 OSI Model
 
Http request&response by Vignesh 15 MAR 2014
Http request&response by Vignesh 15 MAR 2014Http request&response by Vignesh 15 MAR 2014
Http request&response by Vignesh 15 MAR 2014
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 

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