Misspelling, Missing, or Misusing Python Keywords
Here are resources for more information about Python keywords:
00:00 Misspelling, Missing, or Misusing Python Keywords. Python keywords are a set of protected words that have special meaning in Python. These are words that you can’t use as identifiers, variables, or function names in your code.
00:15 They’re a part of the language and can only be used in the context that Python allows. There are three common ways that you can mistakenly use keywords: misspelling a keyword, missing a keyword, or misusing a keyword.
Once again, the exception message isn’t that helpful, but the traceback does attempt to point you in the right direction. If you move back from the caret, then you can see that the
in keyword is missing from the
for loop syntax.
01:24 You can also misuse a protected Python keyword. Remember, keywords are only allowed to be used in specific situations. If you use them incorrectly, then you’ll have invalid syntax in your Python code.
If this code were in a file, then Python would also have the caret pointing right to the misused keyword. Here, you can see that the misuse of
continue follows a similar pattern of generating a
SyntaxError, with a different message.
It might be a little harder to solve this type of invalid syntax in Python code because the code looks fine from the outside. If your code looks good, but you’re still getting a
SyntaxError, then you might consider checking the variable name or function name you want to use against the keyword list for the version of Python that you’re using.
The list of protected keywords has changed with each new version of Python. For example, in Python 3.6 you could use
await as a variable name or function name, but as of Python 3.7, that word has been added to the keyword list.
Become a Member to join the conversation.