Warnings About Dangerous Syntax
In this lesson, you’ll learn about new additions to
SyntaxWarning. Python has a
SyntaxWarning that can warn you about dubious syntax that is typically not a
SyntaxError. Python 3.8 adds a few new ones that can help you when you’re coding and debugging.
The difference between
== can be confusing. The latter checks for equal values, while
True only when objects are the same. Python 3.8 will try to warn you about cases when you should use
== instead of
>>> # Python 3.7 >>> version = "3.7" >>> version is "3.7" False >>> # Python 3.8 >>> version = "3.8" >>> version is "3.8" <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="? False >>> version == "3.8" True
It’s easy to miss a comma when you’re writing out a long list, especially when formatting it vertically. Forgetting a comma in a list of tuples will give a confusing error message about tuples not being callable. Python 3.8 additionally emits a warning that points toward the real issue:
>>> [ ... (3, 5) ... (7, 9) ... ] <stdin>:2: SyntaxWarning: 'tuple' object is not callable; perhaps you missed a comma? Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: 'tuple' object is not callable
The warning correctly identifies the missing comma as the real culprit.
Here are a few resources with info on using IPython, the REPL(Read–Eval–Print Loop) tool used in this video:
Become a Member to join the conversation.