123456789101112131415161718192021222324 |
- #!/usr/bin/env python3
- """ Demonstrates the difference between an instance method,
- a class method, and a static method.
- """
- class MyClass:
- # Instance method. Have to instantiate an instance
- # first! And it can change all the stuff that is
- # unique to that instance. (It can also change
- # the class itself, if it wants to, by accessing
- # self.__class__
- def method(self):
- return 'instance method called', self
- # The class method doesn't need an object to be
- # instantiated. It just is.
- @classmethod
- def classmethod(cls):
- return 'class method called', cls
- # Static methods don't even get access to the class
- @staticmethod
- def staticmethod():
- return 'static method called'
|