Skip to main content
The . (dot) operator in Python is the attribute reference operator, used to access, mutate, or delete attributes and methods bound to an object’s namespace. It serves as the primary syntactic mechanism for traversing object hierarchies and resolving names within the scope of a specific instance, class, or module.

Internal Mechanics

When the Python interpreter encounters the dot operator, it translates the syntax into underlying C API calls or specific magic (dunder) method invocations on the object. The behavior changes depending on the context of the operation (access, assignment, or deletion).

1. Attribute Access

Evaluating obj.attr invokes the object’s __getattribute__ method. If __getattribute__ fails to find the attribute, it raises an AttributeError. Python will then catch this exception and invoke the __getattr__ method as a fallback, provided the class implements it.

2. Attribute Assignment

Using the dot operator on the left side of an assignment (obj.attr = value) binds a value to a name within the object’s namespace. This translates to the __setattr__ method.

3. Attribute Deletion

Using the del statement with the dot operator removes the attribute from the object’s namespace, invoking the __delattr__ method.

Attribute Resolution Order

When the dot operator is used for access, Python follows a strict lookup chain to resolve the attribute name. The dot operator searches in the following order:
  1. Data Descriptors: Objects in the class hierarchy that implement both __get__ and __set__ (e.g., @property), resolved by traversing the Method Resolution Order (MRO).
  2. Instance Dictionary: The instance’s local namespace, stored in obj.__dict__.
  3. Class and Base Class Dictionaries: Attributes found in the class or its base classes, traversed together according to the MRO. If the retrieved object is a Non-Data Descriptor (an object implementing only __get__, such as a standard function), the dot operator invokes its __get__ method to return the result. Otherwise, it returns the raw attribute value.
  4. Fallback: The __getattr__ method, if defined on the class.

Method Binding

When the dot operator accesses a function defined within a class hierarchy, the function itself acts as a descriptor. The dot operator triggers the descriptor protocol (__get__), which automatically binds the instance to the first parameter (self) of the function, returning a bound method object rather than a raw function.
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More