← Home

006. Dynamic Typing

Variables are labels, not typed boxes

006. Dynamic Typing

Aryan’s RAM monitor reads process data from multiple sources: sometimes memory usage arrives as an integer (1024), sometimes as a float (1024.5), sometimes as a string from a config file (“1024 MB”).

He wants a debug utility that identifies the type of any value he passes in. In C, this was impossible at runtime — types were erased after compilation. In Python, every object knows its own type.

He writes describe_type() but makes a subtle mistake: instead of asking “what is the type of this value?”, he asks “is this value equal to the type class itself?” Those are very different questions.

value == int checks if value IS the integer class object. type(value) == int checks if value is an INSTANCE of int. Only the second form works for describing runtime types.


💡 Fun fact: Python’s dynamic typing was a deliberate design choice — Guido van Rossum wanted a language where you could prototype ideas rapidly without fighting a type system. Languages like C and Java are statically typed (types checked at compile time); Python checks types at runtime, which is why bugs like this one only appear when the code actually runs.

⚠️ Watch out: The classic beginner mistake is writing value == int to test a type — this compares the value itself to the class object, which is almost always False. Always wrap the value in type(): type(value) == int.

🤔 Think about it: If Python’s type system is so flexible that a variable can hold any type, what happens when you call a method that only exists on str — say value.upper() — but value happens to be an int at that moment?

Learning objectives

  • Understand that Python variables can change type at runtime
  • Use type() to inspect the current type of a value
  • Distinguish between comparing a value to a type class vs using type()

Key concepts

  • dynamic typing
  • type()
  • runtime types

Try it

Concept detail

Python is dynamically typed: a variable can hold any type and can change types at runtime. x = 5 makes x an int. x = “hello” makes x a str. No declaration needed; the variable is just a label.

type(x) returns the class object that x is an instance of: type(42) → <class ‘int’> type(3.14) → <class ‘float’> type(“hi”) → <class ‘str’>

The broken code bug: ‘value == int’ compares the VALUE to the class object. 42 == int is False (42 is not the integer class, it’s an integer). So every real value falls through to “other” — the function is completely broken but runs without errors.

This distinction matters in production code: a function that always returns “other” is silently wrong. Python’s dynamic typing makes these logical errors possible because there’s no compiler to catch type mismatches.

For subclass-aware checks (e.g., bool is a subclass of int), use isinstance() instead. type(True) == int is False, but isinstance(True, int) is True.

Solution

def describe_type(value):
    if type(value) == int:
        return "int"
    elif type(value) == float:
        return "float"
    elif type(value) == str:
        return "str"
    elif type(value) == list:
        return "list"
    else:
        return "other"

Tests

def test_int():
    assert describe_type(42) == "int"

def test_float():
    assert describe_type(3.14) == "float"

def test_str():
    assert describe_type("hello") == "str"

def test_list():
    assert describe_type([1, 2]) == "list"

def test_other():
    assert describe_type((1, 2)) == "other"

def test_int_class_itself_is_other():
    assert describe_type(int) == "other"

Resources