The Python Language Reference, Release 3.9.5 6.3 Primaries Primaries represent the most tightly bound operations of the language. Their syntax is: primary ::= atom | attributeref | subscription | slicing | call An attribute reference is a primary followed by a period and a name: attributeref ::= primary "." identifier The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier. This production can be customized by overriding the __getattr__() method. If this attribute is not available, the exception AttributeError is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects. Subscription of a sequence (string, tuple or list) or mapping (dictionary) object usually selects an item from the collection: subscription ::= primary "[" expression_list "]" The primary must evaluate to an object that supports subscription (lists or dictionaries for example). User-defined objects can support subscription by defining a$_{__getitem__()}$ method. For built-in objects, there are two types of objects that support subscription: If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) If the primary is a sequence, the expression list must evaluate to an integer or a slice (as discussed in the following section). The formal syntax makes no special provision for negative indices in sequences; however, built-in sequences all provide a$_{__getitem__()}$ method that interprets negative indices by adding the length of the sequence to the index (so that$_{x[-1]}$ selects the last item of$_{x}$). The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). Since the support for negative indices and slicing occurs in the object’s$_{__getitem__()}$ method, subclasses overriding this method will need to explicitly add that support. A string’s items are characters. A character is not a separate data type but a string of exactly one character. Subscription of certain classes or types creates a generic alias. In this case, user-defined classes can support subscrip- tion by providing a$_{__class_getitem__()}$ classmethod. 71 6.3. Primaries 6.3.2 Subscriptions 6.3.1 Attribute references