0

I know there is a way to specify the type of argument like this:

def f(x: int)
    return x

but what if I want to pass a function as an argument? For example:

class Sth:
    def __init__(self, x: int, f: XXX):
        self.__x = x
        self.__f = f

def a():
    return True


obj = Sth(2, a)

What do I type in the place of XXX for it to work?

When I do type(a) it shows <class 'function'>, but I can not put 'function' in place of XXX

1 Answers1

0

You type 'Callable'. See reference.

from typing import Callable

def __init__(self, x: int, f: Callable):
    pass
handras
  • 1,548
  • 14
  • 28