[Python-Dev] Can Python implementations reject semantically invalid expressions? (original) (raw)

Glyph Lefkowitz glyph at twistedmatrix.com
Fri Jul 2 06:43:54 CEST 2010


On Jul 2, 2010, at 12:28 AM, Steven D'Aprano wrote:

This question was inspired by something asked on #python today. Consider it a hypothetical, not a serious proposal.

We know that many semantic errors in Python lead to runtime errors, e.g. 1 + "1". If an implementation rejected them at compile time, would it still be Python? E.g. if the keyhole optimizer raised SyntaxError (or some other exception) on seeing this: def f(): return 1 + "1" instead of compiling something which can't fail to raise an exception, would that still be a legal Python implementation?

I'd say "no". Python has defined semantics in this situation: a TypeError is raised.

To me, this seems akin to a keyhole optimizer arbitrarily deciding that

raise TypeError()

should cause the compiler to abort.

If this type of expression were common, it would be within the rights of, for example, a Python JIT to generate a fast path through 'f' that wouldn't bother to actually invoke its 'int' type's 'add' method, since there is no possible way for a Python program to tell the difference, since int.add is immutable.



More information about the Python-Dev mailing list