Interview frequency: …
Short answer
Any disables checking: it accepts any value and allows any operation on it. object also accepts any value but does not allow special operations on it until the type is narrowed.
Explanation
Any is "contagious": any value derived from it becomes Any and errors disappear until runtime. Use object when your function accepts anything but knows nothing about it (such as logging helpers), and use Any only for integrating with undescribed code or during a gradual migration. The strict settings in mypy or Pyright help reveal implicit Any spreading.
Code example
from typing import Any
def log_any(value: Any) -> None:
value.foo() # No error — checking is disabled
def log_obj(value: object) -> None:
value.foo() # mypy: "object" has no attribute "foo"
if isinstance(value, str):
print(value.upper()) # Accepted after narrowingCommon mistakes
- Using Any as a quick fix for every type error, so the project loses the benefit of checking.
- Using dict[str, Any] to represent data of a known shape instead of a TypedDict or dataclass.
Follow-up questions
- Are type hints enforced at runtime in Python?
- How do you express a value that may be one of two types or None?