It is interesting to note that $ might be an allowed character in variable names in other languages.
A syntax error where we end up using a reserved key-word.
class = "invalid variable name"
This will end up with the same Syntax Error.
$ python syntax_errors/invalid_variable_3.py
File "syntax_errors/invalid_variable_3.py", line 1
class = "invalid variable name"
^
SyntaxError: invalid syntax
Errors while overriding builtins
Keywords are the building blocks of the language. Besides keywords, python includes set of builtins terms that any which are defined already by the interpreter. Python will not through a SyntaxError for overriding a builtin, but it will result in a unexpected run-time error or sometimes mysterious behavior.
Here is example program wherein we have overridden the builtin variable len.
print(len("a string"))
len = len("a string")
print(len("a string"))
This the error printed by the program.
$ python syntax_errors/using_builtin_variable_4.py
8
Traceback (most recent call last):
File "syntax_errors/using_builtin_variable_4.py", line 5, in <module>
print(len("a string"))
TypeError: 'str' object is not callable
The first time, len is used, it will give the length of the string, the second time, the len is used, it will give a TypeError and not SyntaxError.
Fixing Syntax Errors
# the numbers can occur places other than begining of the variable name.
var1 = "invalid variable name"
v2ar = "valid"
# Only _ is the special character allowed
var_ = "valid"
var_name = "valid name"
# We should not be using keywords
no_keywords = "valid"
# We should not be overwriting a builtin
len_of_sentence = len("something")
And exercising this program will not give any syntax errors.
The list of reserved by python keywords can be examined using the help() command in the interpreter.
Go to the interactive help()
Type keywords
$ python
Python 3.8.1 (default, Jan 11 2020, 10:10:32)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help()
Welcome to Python 3.8's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.8/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
help> keywords
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
help>
Similarly, the builtins can be listed using the interpreter using the dir() call on __builtins__.
Our test driven programs will not have a SyntaxError or errors due to overwriting builtin names. These errors are easiest to catch and should be fixed immediately.