Search notes:

Python: callable objects

An object becomes callable if it implements the __call__ method. It then behaves similar to a function:
class multiplier:

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

     def __call__(self, v):
         return self.t * v


mult_by_4 = multiplier(4)
mult_by_9 = multiplier(9)

print(mult_by_4(5)) # 20
print(mult_by_9(3)) # 27
Github repository about-Python, path: /objects/callable/demo.py

See also

The functools module.
Callable types

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...', 1745040465, '3.144.158.54', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/objects/callable(54): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78