I am trying to properly type hint my code and encountered both Callable and FunctionType
from typing import Callable
def my_func() -> Callable:
f = lambda: _
return f
result = my_func()
type(result) # <class 'function'>
isinstance(result, Callable) # True
vs
from types import FunctionType
def my_func() -> FunctionType
f = lambda: ...
return f
result = my_func()
type(result) # <class 'function'>
isinstance(result, FunctionType) # True
One possible case I can think of is to distinguish between regular and class-based callables like this
class C:
def __call__(self):
pass
def my_func() -> Callable:
c = C()
return c
result = my_func()
type(result) # <class 'function'>
isinstance(result, Callable) # True
isinstance(result, FunctionType) # False
What are the differences between those and when I have to use one over the other?