Skip to main content

object

Properties

>>> dir(object)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Definition

object is the base class of all new-style classes in Python. Every class ultimately inherits from object, which provides default implementations for core behaviors like representation, equality, hashing, and attribute access.

>>> class X: pass
>>> isinstance(X(), object)
True

Subclassing object

Explicitly inheriting from object is optional in Python 3, but the behaviors come from it regardless.

class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y

Attribute access hooks

  • __getattribute__ — intercepts all attribute access.
  • __setattr__ / __delattr__ — customize assignment/deletion.

Use with care to avoid infinite recursion; delegate to super().__getattribute__ as needed.

Descriptor protocol

Objects implementing __get__, __set__, or __delete__ act as descriptors (used by properties, methods, etc.).

Dunder methods

Dunder MethodOperationExample (normal syntax)Example (dunder call)
__init__Initializeobject()object().__init__()
__str__String representationstr(object())object().__str__()
__repr__Developer representationrepr(object())object().__repr__()
__eq__Equalityobject() == object()object().__eq__(object())
__hash__Hash valuehash(object())object().__hash__()
__getattribute__Attribute lookupobj.attrobj.__getattribute__('attr')
__setattr__Attribute setobj.attr = 1obj.__setattr__('attr',1)
__delattr__Attribute deletedel obj.attrobj.__delattr__('attr')
Dunder MethodOperationExample (normal syntax)Example (dunder call)
__get__Access attributeobj.xprop.__get__(obj, type(obj))
__set__Assign valueobj.x = 5prop.__set__(obj, 5)
__delete__Delete valuedel obj.xprop.__delete__(obj)