if True # Bad Syntax
print("hello")Cell In[1], line 1 if True # Bad Syntax ^ SyntaxError: expected ':'
SyntaxErrorCell In[1], line 1 if True # Bad Syntax ^ SyntaxError: expected ':'
- Errors in literals can also be caught
Not likely to get much than that
Because python is dynamic, variable definitions and lifetimes are difficult to track
Means that something obviously wrong like below, is not strictly invalid, so can’t be flagged as an error at parse time
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) Cell In[4], line 1 ----> 1 bad_reference() Cell In[3], line 2, in bad_reference() 1 def bad_reference(): ----> 2 print(local_var) 3 local_var = 123 UnboundLocalError: cannot access local variable 'local_var' where it is not associated with a value
x is valid, sometimes notx, every work fine--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) Cell In[7], line 1 ----> 1 sometimes_ok(False) Cell In[5], line 4, in sometimes_ok(x) 2 if x: 3 local_var = 123 ----> 4 print(local_var) UnboundLocalError: cannot access local variable 'local_var' where it is not associated with a value
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[8], line 5 1 def bad_math(): 2 return 1 / 0 ----> 5 bad_math() Cell In[8], line 2, in bad_math() 1 def bad_math(): ----> 2 return 1 / 0 ZeroDivisionError: division by zero
Can’t immediately infer this is wrong because the / operator might have been overloaded
Some other problems python will fail to statically detect are
Linting tools like Flake 8 and Ruff (see above) can help catch these
Even with these tools its important to be aware that most python errors will be caught at run time
Therefore it’s important to,