object()
The root of the type hierarchy. Everything inherits from it; calling it gives a unique sentinel.
Common call
MISSING = object()
Returns
a fresh, unique instance
Replaces
None or a magic string as a sentinel
Watch out
object instances have almost no attributes; you cannot set attributes on them (no __dict__)
object()
→ object
Common patterns
Sentinel to distinguish "not provided" from None
When None is a valid argument value, you need a different sentinel to detect "caller did not pass anything".
_MISSING = object() def fetch(key, default=_MISSING): if key in cache: return cache[key] if default is _MISSING: raise KeyError(key) return default
Base for a hierarchy — usually implicit
In Python 3, `class C:` implicitly means `class C(object):`. Explicit inheritance is rare but harmless.
class Base(object): # same as class Base: ...
Distinguish objects by identity, not equality
Every object() is unique — `is` never lies.
a = object() b = object() a is b # False
Examples
1. Fresh instance
object()
Returns
<object object at 0x...>2. Unique each time
object() is object()
Returns
False3. Every class inherits
issubclass(str, object)
Returns
True4. MRO ends at object
int.__mro__
Returns
(int, object)5. No writable dict
x = object()
x.foo = 1
Returns
AttributeError: 'object' object has no attribute 'foo'6. Sentinel pattern
_M = object()
def f(x=_M): ...
Returns
_M can never be confused with a caller valuePitfalls
1. You cannot set attributes on an object() instance
object instances have no __dict__. Attempting to attach an attribute raises AttributeError. For a lightweight class with attributes, use `class C: pass` (a bare user class DOES have __dict__).
No __dict__
x = object() x.attr = 1
AttributeError: 'object' object has no attribute 'attr'
Bare class
class Bag: pass x = Bag() x.attr = 1
1
2. None as a sentinel is often ambiguous
If your API accepts None as a real value (e.g. "set to None to clear"), you cannot use None to mean "caller did not pass anything". A unique object() gives you an unambiguous sentinel.
Ambiguous None
def f(x=None): if x is None: ... # caller passed None or nothing?
ambiguous
Sentinel
_MISSING = object() def f(x=_MISSING): if x is _MISSING: ...
clear
3. Explicit inheritance from object is redundant in Python 3
`class C(object):` and `class C:` are IDENTICAL in Python 3. The explicit form is a Python 2 vestige (where it distinguished new-style from old-style classes). Keep or drop it consistently; do not mix.
Legacy noise
class MyClass(object): ... # Python 2 style
works, but noisy
Modern style
class MyClass: ...
same, cleaner
4. Not all Python objects are instances of object at the CLASS level
This is a subtle detail: object is a class, but classes themselves are instances of type, and type is an instance of type. The class-vs-instance hierarchy loops in a way that is easy to get wrong. For everyday code, remember: `isinstance(anything, object)` is True.
Overreach
isinstance(type, object)
True — but type IS object, not just an instance
Everyday truth
isinstance(x, object) # True for anything
always True
When to use
Use it
- Sentinel values that must never collide (default marker in a function signature)
- Rare cases where you want a hashable, unique, comparable-by-identity value
- As the (usually implicit) base class of your own classes
Reach for something else
- You need attributes → use a bare `class C: pass` instead
- You need equality by value → use a dataclass or explicit class
- You want to see if it's an object → isinstance(x, object) is always True
- Explicit inheritance from object in Python 3 code — clean it up
Notes
Complexity
O(1) construction
Return
A new object() instance — unique per call
CPython impl
Objects/object.c :: object_new
Memory
Minimal — just the header
Thread-safe
Yes
FAQ
Almost exclusively for sentinel values — `_MISSING = object()` gives you a value guaranteed to be distinct from anything a caller could pass. Otherwise, object is used implicitly as the base of every class.
History
2.2
object introduced as the root of the "new-style" class hierarchy.
3.0
All classes implicitly inherit from object; the old-style class system was removed.