[Python-Dev] Can Python implementations reject semantically invalid expressions?

Craig Citro craigcitro at gmail.com
Fri Jul 2 18:51:08 CEST 2010


> But you would be taking a module that will compile and making it uncompilable.
>

You're absolutely right, and since I definitely *don't* think that the
program "raise TypeError" should cause a CompileError, you could say
it's safer to have a simple rule like "vaild syntax => will compile"
-- it's probably a slippery slope once you start deciding which bits
of semantics raise CompileErrors and which don't.

However, in this particular case, here's a question: *why* would
someone write "return 1 + '1'"? Did they do it *knowing* what would
happen, or because they just didn't realize it was just an error?

 * If they knew what it was going to do, then I'd say shame on them --
they should have just raised a TypeError instead, and anyone who comes
along to read or maintain that code would thank them for the change.
My impression is that we generally try to discourage people from
writing "tricky" code with Python, and this would do exactly that.

 * If, on the other hand, it's an accident, then I think it's a
service to the user to let them know as soon as possible. Here I'm
thinking both of someone new to Python, or even a seasoned pro who
makes a few "quick fixes" before sending some code to someone, and
doesn't happen to test that code path before handing it off.

Either way, I personally prefer the CompileError -- it helps me catch
a stupid mistake if I've made one, and it prevents other people from
writing code I find less clear.

My real motive, though, is that I'd like to have more freedom for
Python implementations, *especially* things that let you make more
decisions at compile-time. (This is no doubt influenced by the fact
that I've spent a lot of time thinking about Cython lately.) In this
case, I see it as a win-win -- it gives more freedom to the folks
writing the implementation, and (personally) I find it more pleasing
as a user. Again, I don't think this *particular* case allows us to do
something awesome behind the scenes with Cython -- but the community
starting to consider changes of this ilk *would* be a big win, I
think.

-cc


More information about the Python-Dev mailing list