callable-object

Injecting a callable object into a class as a method

Injecting a callable object into a class as a method Question: It is possible to inject a function into a class like this: class MainClass: … def simple_injected_func(self: MainClass, arg: str) -> None: print(f"simple_injected_func({arg})") MainClass.simple_injected_func = simple_injected_func main_object = MainClass() main_object.simple_injected_func("arg") # outputs: simple_injected_func(arg) Furthermore it is possible to make an object callable like this …

Total answers: 1

Python how to type hint a Callable with __wrapped__

Python how to type hint a Callable with __wrapped__ Question: When passing around functions, I normally type hint them with typing.Callable. The docs for collections.abc.Callable state that it has four dunder methods: class collections.abc.Callable ABCs for classes that provide respectively the methods __contains__(), __hash__(), __len__(), and __call__(). At one point, I want to check if …

Total answers: 2

What is the difference between __init__ and __call__?

"__init__" vs "__call__" in python class Question: I want to know the difference between __init__ and __call__ methods. For example: class test: def __init__(self): self.a = 10 def __call__(self): b = 20 Asked By: sam || Source Answers: __init__ would be treated as Constructor where as __call__ methods can be called with objects any number …

Total answers: 17