Search notes:

Python: class methods

A method is a class

A method is a class. So it can be assigned to variable, and then later be called:
class cls:

      def __init__(self, ident):
          self.ident = ident

      def x(self):
          print('x was called, ident = {}'.format(self.ident))

      def y(self):
          print('y was called, ident = {}'.format(self.ident))


obj_1 = cls('foo')
obj_2 = cls('bar')

m1 = obj_1.x
m2 = obj_2.y

m1()
#
#  x was called, ident = foo

m2()
#
#  y was called, ident = bar

print(type(m1))
#
#  <class 'method'>

Github repository about-Python, path: /class/methods/a-method-is-a-class.py

See also

The __mro__ dunder (which stands for method resolution order).

Index