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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199138, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/class/methods/index(70): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78