Proposal: local variable declarations and type advice

Raymond Hettinger othello
Wed Feb 27 01:36:22 EST 2002


"Paul Rubin" <phr-n2002a at nightsong.com> wrote in message
news:7xpu2slwtl.fsf_-_ at ruckus.brouhaha.com...
<snipped>
>    Example 2:
>      local x(int)
>      x = "banana"    # implementation-dependent behavior
>
>    Some ways Python can handle example 2:
>
>    1a) Compiler does enough static analysis to flag the "banana"
assignment
>       as a compile time error
>
>    1b) Compiler doesn't flag; interpreter raises runtime TypeError
exception.
>
>    2) Compiler doesn't flag; interpreter assigns "banana", i.e. the
>       advice is completely ignored and does nothing, the program works
>       exactly the same way it does now without the advice
>
>    3) Compiler doesn't flag, interpreter has totally undefined behavior.
>       It can work normally, or crash, or delete all your files, etc.
>       The idea is that an optimizing compiler can believe the advice and
>       generate code based on x always being an int (it can put it in a
>       register, perform native machine code arithmetic on it etc).
>

How about another option:

4) Code is generated along the lines of:
if type('banana')==int:
   <run optimized code that assumes 'banana' to be an int>
elseif USESTRICT:
   raise TypeError
else:
   <run normal, slow code which binds 'banana' to int regardless of type>

In other words, generate optimized code in case the type advice is correct;
otherwise, run normally unless strict type checking is in effect.

This allows optimization based on the advice and error checking only when
wanted.

In general, I like your proposals (after Greg Ewing's correction).  It
reminds
of importing packages in that it is a simple tool for building large, fast,
reliable
programs while staying out of the way (and out of mind) for more casual
programming.


Raymond Hettinger







More information about the Python-list mailing list