[Python-Dev] Code Generation Idea Was: Bytecode idea

Paul Hughett Paul Hughett <hughett@bbl.med.upenn.edu>
Wed, 26 Feb 2003 11:04:12 -0500


    Guido> Problem with this is that there's currently lots of code out
    Guido> there that was recently modified to read

    Guido>   try:
    Guido>     True
    Guido>   except NameError:
    Guido>     True = 1
    Guido>     False = 0

    Guido> That would become a syntax error if True/False were true
    Guido> constants. :-(

> Hmmm...  Those code blocks would only be executed on older versions of the
> interpreter.  Is there some way to use that knowledge to finesse the
> problem?  It would be a real hack, but if the compiler recognized precisely
> the above construct (or at least assignment in an except block guarded by
> NameError), it could shut up about the assignment.

Would it be simpler to institute a special rule that True = 1 is silently
ignored, but True = anything else generates an error message?  Or am I
overlooking something important?

If you want to handle assigning to True an expression whose value is
known only at run time, translate it into

   if expression != 1 :
      raise Exception, "Attempt to assign invalid value to True"

which would give a run-time error without costing cycles anywhere else.


Paul Hughett