Variables and Memory · Lecture 04 · 6 min

Dynamic vs Static Typing

In Python, types belong to objects, not to variable names. A name can refer to an object of any type at any time.

Python loads when you run a cell Shift+Enter runs a cell. Cells share variables, like a notebook.

Static typing

In statically typed languages such as Java, C or C++, you declare a variable’s type up front, and it stays that type:

String myVar = "hello";
myVar = 10;        // compile error: incompatible types
myVar = "abc";     // fine: still a String

The variable carries a type, and the compiler enforces it.

Dynamic typing

Python is dynamically typed. A variable name has no type of its own. It’s just a reference, and the object it refers to has the type. type(name) reports the type of whatever object the name points to right now:

a = 'hello'
type(a)
a = 10
type(a)
a = lambda x: x ** 2
print(a(2))
type(a)

The name a referred to a str, then an int, then a function. No type was ever attached to a itself. Each assignment simply pointed the name at a different object.

What about type hints?

You’ll often see code like count: int = 0 or def greet(name: str) -> str:. These annotations are documentation for humans and for tools like mypy, pyright and IDEs. Python itself does not enforce them when your code runs:

count: int = 0
count = 'not a number'   # no error at run time
type(count)

So Python stays dynamically typed even when you use type hints. You’ll meet annotations properly in the section on functions.

Key takeaways

  • In Python, objects have types; names don’t.
  • type(x) tells you the type of the object x currently refers to.
  • Type hints are not enforced at run time. They help readers and static checkers.