Is this a "gotcha" in Python?

Jon Ribbens jon+usenet at unequivocal.eu
Sat Apr 20 06:56:34 EDT 2019


On 2019-04-19, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>   Now consider the same in Python:
>
> def f():
>     # ... 
>     l = 22 # representing a length
>     # ... 
>     l = 'abc'; # representing the left half of something
>     # ...
>
>   A Python implementation does not catch the "error".

Obviously it is a deliberate design decision that Python does not
require declaration of variables.

>   Has this ever been a problem for someone?

It would be astonishing if it had not.

>   Are there means to deal with this?

You would hope that pylint would catch it, but it appears it does not.
mypy --strict will catch it if the reassignment has a different type.


More information about the Python-list mailing list