Classesobjects 190509101857
Classesobjects 190509101857
Team Emertxe
Creation of Class
Creation of Class
General Format
Class is a model or plan to create the objects
Class contains,
Attributes: Represented by variables
Actions : Performed on methods
Syntax of defining the class,
Syntax Example
class Classname(object): class Student:
"""docstrings""" """The below block defines attributes"""
def __init__(self):
Attributes self.name = "Ram"
self.age = 21
def __init__(self): self.marks = 89.75
def method1():
def method2(): """The below block defines a method"""
def putdata(self):
print("Name: ", self.name)
print("Age: ", self.age)
print("Marks: ", self.marks)
Creation of Class
Program
#To define the Student calss and create an Object to it.
#Class Definition
class Student:
#Special method called constructor
def __init__(self):
self.name = "Ram"
self.age = 21
self.marks = 75.90
‘Self’ is the default variable that contains the memory address of the instance of the
current class
Constructor will be called only once i.e at the time of creating the objects
s = Student()
Constructor
Constructor with parameter
class Student:
#Constructor definition
def __init__(self, n = "", m = 0):
self.name = n
self.marks = m
#Instance method
def putdata(self):
print("Name: ", self.name)
print("Marks: ", self.marks)
Instance variables
Class / Static variables
Types Of Variables
Instance Variables
Variables whose separate copy is created for every instance/object
These are defined and init using the constructor with 'self' parameter
Accessing the instance variables from outside the class,
instancename.variable
Single copy is created for all instances
Accessing class vars are possible only by 'class methods'
Accessing class vars from outside the class,
classname.variable
If class vars are modified in class namespace, then it reflects to all instances
Namespaces
Instance Namespace
#To understand class namespace Before modifyng class variable ‘n’
s1 = Student() 10 n 10
n
s2 = Student()
11 n 10
n
Types:
Instance Methods
- Accessor
- Mutator
Class Methods
Static Methods
Types of Methods
Instance Methods
●
Acts upon the instance variables of that class
●
Invoked by instance_name.method_name()
#To understanf the instance methods #Constructor called without any parameters
s = Student()
class Student: s.putdata()
#Constructor definition
def __init__(self, n = "", m = 0): #Constructor called with parameters
self.name = n s = Student("Ram", 99)
self.marks = m s.putdata()
#Instance method
def putdata(self):
print("Name: ", self.name)
print("Marks: ", self.marks)
Types of Methods
Instance Methods: Accessor + Mutator
Accessor Mutator
● Methods just reads the instance variables, ● Not only reads the data but also modifies
will not modify it it
● Generally written in the form: getXXXX() ● Generally wriiten in the form: setXXXX()
● Also called getter methods ● Also called setter methods
#Define accessor
def getName(self):
return self.name
Types of Methods
Class Methods
● This methods acts on class level
● Acts on class variables only
● Written using @classmethod decorator
● First param is 'cls', followed by any params
● Accessed by classname.method()
class Bird:
#Define the class var here
wings = 2
#Call
Bird.fly("Sparrow")
Bird.fly("Pigeon")
Types of Methods
Static Methods
● Needed, when the processing is at the class level but we need not involve the class or
instances
● Examples:
- Setting the environmental variables
- Counting the number of instances of the class
● Static methods are written using the decorator @staticmethod
● Static methods are called in the form classname.method()
def putdata(self):
print("Name: ", self.name)
print("Salary: ", self.salary)
1. To calculate the power value of a number with the help of a static method
Inner Class
Inner Class
Introduction
● Creating class B inside Class A is called nested class or Inner class
● Example:
Person's Data like,
- Name: Single value
- Age: Single Value
- DoB: Multiple values, hence separate class is needed
Inner Class
Program: Version-1
def display(self):
print("DoB: {}/{}/{}" . format(self.dd,
self.mm, self.yy))
Inner Class
Program: Version-2
def display(self):
print("DoB: {}/{}/{}" . format(self.dd,
self.mm, self.yy))
THANK YOU