Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type hint for a function [duplicate]

Is it just callable? In the meantime I have been using function as my IDE regards callable as a bool.

def func(a: int, b: str, callback: ???)
like image 546
Matt Avatar asked Aug 26 '17 15:08

Matt


People also ask

What is type hint?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5.

What type is a function in Python?

The name of this function is Python type(). Python type() function is a built-in function in python that allows the programmer to evaluate the type of data used in the program for every variable.

What is type annotation in Python?

What Are Type Annotations? Type annotations — also known as type signatures — are used to indicate the datatypes of variables and input/outputs of functions and methods. In many languages, datatypes are explicitly stated. In these languages, if you don't declare your datatype — the code will not run.

How do you write a class hint in Python?

In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing. Type ).


1 Answers

Yes, typing.Callable is the right hint for a callback.

Also see the Callable section of PEP 484:

Frameworks expecting callback functions of specific signatures might be type hinted using Callable[[Arg1Type, Arg2Type], ReturnType].

like image 93
Martijn Pieters Avatar answered Oct 19 '22 08:10

Martijn Pieters