Source Code:
(back to article)
class MyClass: def __init__(self, value): self.value = value # instance method def instance_method(self): return f'instance method called, value={self.value}' # static method @staticmethod def static_method(): return 'static method called' # class method @classmethod def class_method(cls): return f'class method called, cls={cls.__name__}' # create an instance of MyClass obj = MyClass(value=10) # call instance method on instance print(obj.instance_method()) # Output: 'instance method called, value=10' # call instance method on class print(MyClass.instance_method(obj)) # Output: 'instance method called, value=10' # call static method on instance print(obj.static_method()) # Output: 'static method called' # call static method on class print(MyClass.static_method()) # Output: 'static method called' # call class method on instance print(obj.class_method()) # Output: 'class method called, cls=MyClass' # call class method on class print(MyClass.class_method()) # Output: 'class method called, cls=MyClass'
Result:
Report an issue