Next: Weakened Type Checking, Previous: Compile Time Type Errors, Up: Types in Python [Contents][Index]
With the default compilation policy, all type
assertions6 are precisely
checked. Precise checking means that the check is done as though
typep
had been called with the exact type specifier that
appeared in the declaration. Python uses policy to determine
whether to trust type assertions (see compiler-policy). Type
assertions from declarations are indistinguishable from the type
assertions on arguments to built-in functions. In Python, adding
type declarations makes code safer.
If a variable is declared to be (integer 3 17)
, then its
value must always always be an integer between 3
and 17
.
If multiple type declarations apply to a single variable, then all the
declarations must be correct; it is as though all the types were
intersected producing a single and
type specifier.
Argument type declarations are automatically enforced. If you declare
the type of a function argument, a type check will be done when that
function is called. In a function call, the called function does the
argument type checking, which means that a more restrictive type
assertion in the calling function (e.g., from the
) may be lost.
The types of structure slots are also checked. The value of a
structure slot must always be of the type indicated in any :type
slot option.7 Because of precise type checking,
the arguments to slot accessors are checked to be the correct type of
structure.
In traditional Common Lisp compilers, not all type assertions are checked, and type checks are not precise. Traditional compilers blindly trust explicit type declarations, but may check the argument type assertions for built-in functions. Type checking is not precise, since the argument type checks will be for the most general type legal for that argument. In many systems, type declarations suppress what little type checking is being done, so adding type declarations makes code unsafe. This is a problem since it discourages writing type declarations during initial coding. In addition to being more error prone, adding type declarations during tuning also loses all the benefits of debugging with checked type assertions.
To gain maximum benefit from Python’s type checking, you should
always declare the types of function arguments and structure slots as
precisely as possible. This often involves the use of or
,
member
and other list-style type specifiers. Paradoxically,
even though adding type declarations introduces type checks, it
usually reduces the overall amount of type checking. This is
especially true for structure slot type declarations.
Python uses the safety
optimization quality (rather than
presence or absence of declarations) to choose one of three levels of
run-time type error checking: see optimize-declaration.
See advanced-type-stuff for more information about types in
Python.
There are a few circumstances where a type declaration is discarded rather than being used as type assertion. This doesn’t affect safety much, since such discarded declarations are also not believed to be true by the compiler.
The initial value need not be of this type as
long as the corresponding argument to the constructor is always
supplied, but this will cause a compile-time type warning unless
required-argument
is used.
Next: Weakened Type Checking, Previous: Compile Time Type Errors, Up: Types in Python [Contents][Index]