0% found this document useful (0 votes)
9 views3 pages

31 July

The document explains different types of methods in a class, specifically instance methods, class methods, and their usage with examples. It details how instance methods use 'self' to refer to the invoking object, while class methods utilize the '@classmethod' decorator and 'cls' to access class variables. Additionally, it covers the implementation of getter and setter methods for object attributes.

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

31 July

The document explains different types of methods in a class, specifically instance methods, class methods, and their usage with examples. It details how instance methods use 'self' to refer to the invoking object, while class methods utilize the '@classmethod' decorator and 'cls' to access class variables. Additionally, it covers the implementation of getter and setter methods for object attributes.

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# 31-July

'''
Types Of methods in class
1. instance Method
2. Static Method
3. Class Method
'''
'''
# instance method ( object )
1. in instance method we pass or use 'self' variable
( 'self' is refer to invoking object )
2. In instance method we always write coding related to object
3. we can call instance method method using invoking object
# Ex: d1.getdata() 'd1' is invoking object
'''
'''
# Example of instance method
class Student:
def getdata(self): # instance method , here 'self' is refer to invoking obj
print("Enter Sno Sname and Marks")
self.sno = int(input())
self.sname= input()
self.m = int(input())
def showdata(self): # instance method ,
print(self.sno," ",self.sname," ",self.m)
#self: refer to invoking object

#main
s1 = Student() # creating object
s2 = Student() # creating object
s1.getdata() # s1 is invoking object
s2.getdata() # s2 is invoking object
s1.showdata()
s2.showdata()
'''
'''
# Eaxmple of instance method
class Student:
def __init__(self,n,m): # parameterized constructor
self.name = n
self.marks = m
def display(self): # instance method
print('Hi',self.name)
print('Your Marks are:',self.marks)
def grade(self): # instance method
#self.display() # Calling display() method for invoking obj
if self.marks>=60:
print('You got First Grade')
elif self.marks>=50:
print('Yout got Second Grade')
elif self.marks>=35:
print('You got Third Grade')
else:
print('You are Failed')
#main
n = int(input('Enter number of students:')) # 5 or 300
l_lst = [] # empty list
for i in range(n): # 'n' times
n1 = input('Enter Name:')
m1= int(input('Enter Marks:'))
s = Student(n1,m1) # creating object
l_lst.append(s)

print('RECORD OF ALL STUDENTS \n' )


for s1 in l_lst:
s1.display()
s1.grade()
'''
# Getter and Setter
# Getter : A instance method which is returning value of object
# Setter : A instance method which is Set the value of object
'''
# Example Of Getter and Setter
class Demo:
def setxy(self,a,b): # setter
self.x=a
self.y=b
def getxy(self): # getter
return self.x,self.y # returning multiple values
# main

d1 = Demo() # creating object


d1.setxy(10,20)
a,b=d1.getxy()
print(a,b) # O/P 10 20
'''

# 2. class Method
'''
• Inside of method implementation if we are using only class variables (static
variables),
then such type of methods we should declare as class method.
• We can declare class method explicitly by using @classmethod decorator.
• For class method we should provide 'cls' variable at the time of declaration
• We can call classmethod by using classname.
'''
'''
# Example of class method
class Student:
college = "SAM" # static variable
def __init__(self,n,m): # constructor
self.sname=n
self.marks=m
def display(self): # instance method
print(self.sname,self.marks)

@classmethod
def showCollege(cls): #class method
print(Student.college)
# main
s1 = Student("pranshu",244)
s2 = Student("Aakash",233)
s1.display()
Student.showCollege() # Calling class method
s2.display()
Student.showCollege() # Calling class method
'''
'''
# Program to track the number of objects created for a class:
# counting total no object in a program

class Test:
count=0 # static variable
def __init__(self):
Test.count = Test.count+1
print(Test.count , ' Object Is Created ')
self.x=10

@classmethod
def noOfObjects(cls):
print('The number of objects created for test class:',Test.count)
# print(self.x) # Error( Cant use instance variable )

Test.noOfObjects() # 0
t1=Test()
t2=Test()
Test.noOfObjects() # 2
t3=Test()
t4=Test()
t5=Test()
Test.noOfObjects() # 5
'''

You might also like