Special Method Protocols

__call__() — class creation protocol

Make instances callable with parentheses. Arguments are passed to the hook; it can maintain state between calls. On a metaclass, __call__ instead controls class instance construction. The displayed signature is the concrete example contract; constructor or call parameters depend on the class.

Formal Syntax & Structure

python syntaxpython
def __call__(self, value):

Parameters & Attributes

NameTypeDescription
valueobjectExample call argument.

Return Value

object

Result of the call.

Working Example

Example Snippetpython
class Scale:    def __init__(self, factor): self.factor=factor    def __call__(self, value): return self.factor*valueprint(Scale(3)(7))
Python Reference