types of methods in oop python
# Instance Method Example in Python
class Student:
def __init__(self, a, b):
self.a = a
self.b = b
def avg(self):
return (self.a + self.b) / 2
s1 = Student(10, 20)
print( s1.avg() )
0
1
# Static Method Implementation in python
class Student:
name = 'Student'
def __init__(self, a, b):
self.a = a
self.b = b
@staticmethod
def info():
return "This is a student class"
print(Student.info())
Thank you!
1
0
0
5
# Class Method Implementation in python
class Student:
name = 'Student'
def __init__(self, a, b):
self.a = a
self.b = b
@classmethod
def info(cls):
return cls.name
print(Student.info())
Thank you!
5
0
Are there any code examples left?
New code examples in category Python