[Python-3000] Type Comparisons with Godel Numbers
Giovanni Bajo
rasky at develer.com
Sat Apr 22 00:21:24 CEST 2006
Guido van Rossum <guido at python.org> wrote:
> But let me point out that the key concern I have about the expense of
> type checking is what would be done when unchecked code calls a
> function with type-checked arguments. If I have some utterly dynamic
> code that comes up with a list of a million ints, and then I pass that
> as an argument to a function that requests the argument type is
> list[int], then I don't see how we can prevent the cost of doing a
> million checks to ensure that each and every item in the list is an
> int; either we do it all at once at the call site, or we amortize the
> cost over the lifetime of every list object, whether or not it is ever
> going to be used in a typechecked call. Neither sounds very
> attractive, and AFAICT your approach doesn't solve this particular
> issue.
One way would be to keep boolean flags like "is this a list of integers". It
could be updated after each list modification, so that typechecks come for
(almost) free.
Another (similar) way would be to let the user pay for the high typechecking
price in normal cases *but* provide a list[int] class, which is a list
decorated with typechecks on modification operations. We could have
list[int|float]() to construct a list which can hold either ints or floats.
In both the above cases, you don't pay when you read from the list or pass it
to a typechecked function, you pay only when you *modify* it. I imply that all
these checks will be disabled at -O.
Giovanni Bajo
More information about the Python-3000
mailing list