Why syntax error messages for some built-in functions are different?

154 views Asked by At

I recently noticed something weird. I typed something like

exec code

And I obviously got an error. What is interesting is the error description - SyntaxError: Missing parentheses in call to 'exec'. I haven't found any other function that would give similarly detailed one. If I have for example

eval code

I get SyntaxError: invalid syntax. All user-defined functions and all built-ins I found, e. g. min, filter, behave the same.

Why are these error messages inconsistent?

2

There are 2 answers

1
DeepSpace On BEST ANSWER

Since exec code is a valid syntax in Python 2 but not in Python 3, this error message is more detailed than a general SyntaxError because it was (and is) very popular (well, as popular as the usage of exec) when one is transitioning from Python 2 to Python 3.

You get the exact error when trying to print string in Python 3 (as long as string is defined of course).

0
Dimitris Fasarakis Hilliard On

The difference in errors exists because eval was (in 2.x) and still is (in 3.x) a function call. exec, on the other hand, was a statement in 2.x and made into a function in 3.x.