Search code examples
pythontypescastingvariable-assignment

Is it possible to get an error when accidentally assigned wrong type in python?


For example I have next code:

name = str('John Doe')

Let's imagine after I assign to name = 1, but code still valid. Is it possible to get an error in this case in Python or within some special tool?


Solution

  • Python is dynamically typed, so you can assign pretty much anything to anything. This would work:

    name = 1
    name = "Bill"
    

    You can add in type checking with mypy