Next: 5.2.7 The Values Declaration
Up: 5.2 More About Types
Previous: 5.2.5 The Empty Type
function types are understood in the restrictive sense, specifying:
- The argument syntax that the function must be called with. This
is information about what argument counts are acceptable, and which
keyword arguments are recognized. In Python, warnings about
argument syntax are a consequence of function type checking.
- The types of the argument values that the caller must pass. If
the compiler can prove that some argument to a call is of a type
disallowed by the called function's type, then it will give a
compile-time type warning. In addition to being used for
compile-time type checking, these type assertions are also used as
output type assertions in code generation. For example, if
foo is declared to have a fixnum argument, then the
1+ in (foo (1+ x)) is compiled with knowledge that
the result must be a fixnum.
- The types the values that will be bound to argument variables in
the function's definition. Declaring a function's type with
ftype implicitly declares the types of the arguments in the
definition. Python checks for consistency between the definition
and the ftype declaration. Because of precise type checking,
an error will be signalled when a function is called with an
argument of the wrong type.
- The type of return value(s) that the caller can expect. This
information is a useful input to type inference. For example, if a
function is declared to return a fixnum, then when a call to
that function appears in an expression, the expression will be
compiled with knowledge that the call will return a fixnum.
- The type of return value(s) that the definition must return.
The result type in an ftype declaration is treated like an
implicit the wrapped around the body of the definition. If
the definition returns a value of the wrong type, an error will be
signalled. If the compiler can prove that the function returns the
wrong type, then it will give a compile-time warning.
This is consistent with the new interpretation of function types and
the ftype declaration in the proposed X3J13
``function-type-argument-type-semantics'' cleanup. Note also, that if
you don't explicitly declare the type of a function using a global
ftype declaration, then Python will compute a function type
from the definition, providing a degree of inter-routine type
inference, see section 5.3.3.
Next: 5.2.7 The Values Declaration
Up: 5.2 More About Types
Previous: 5.2.5 The Empty Type
Raymond Toy
Mon Jul 14 09:11:27 EDT 1997