instance method
class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y
def sum(self):
return self.x + self.y
In this example, sum()
is an instance method of the MyClass
class. It takes the instance itself (self
) as the first argument, and returns the sum of x
and y
. To call this method, we first need to create an instance of the class, and then use the dot notation to call the method on that instance:
obj = MyClass(3, 5)
print(obj.sum()) # Output: 8
Last updated