Solved

Isn't dynamic typing dangerous?

ElaKowalskaOP

In the example x is first a number, then a string. In Java that would never compile. Doesn't this lead to bugs?

11

Comments 5

Join the discussion

Already have an account? Log in

  1. BellaRossi

    It can, yes. The trade-off is flexibility and less boilerplate vs. catching some mistakes earlier.

    2
    1. OllieGrant Solutionreplies: 2

      Two things help a lot:
      1. Don't reuse one variable for different kinds of data. That's a style choice, not a language rule.
      2. Type hints: count: int = 0. Python doesn't enforce them at runtime, but editors and tools like mypy will warn you.

      6
      1. ElaKowalskaOPreplies: 1

        Type hints look like a good middle ground. Are they used in real projects?

        0
        1. SophieDev

          All the time in bigger codebases. For learning, don't worry about them yet, but it's nice to know they exist.

          6
      2. NoahLoops

        Coming from C#, the freedom felt scary at first. After a month I stopped noticing.

        4