object. __contains__ ( self , item$_{)}$ Called to implement membership test operators. Should return true if item is in self , false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define$_{__contains__()}$ , the membership test first tries iteration via$_{__iter__()}$ , then the old sequence iteration protocol via$_{__getitem__()}$ , see this section in the language reference . If the$_{__reversed__()}$ method is not provided, the$_{reversed()}$ built-in will fall back to using the sequence protocol ($_{__len__()}$ and$_{__getitem__()}$ ). Objects that support the sequence protocol should only provide$_{__reversed__()}$ if they can provide an implementation that is more efficient than the one provided by$_{reversed()}$ . Called (if present) by the$_{reversed()}$ built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. object. __reversed__ ( self$_{)}$ Iterator objects also need to implement this method; they are required to return themselves. For more infor- mation on iterator objects, see typeiter. This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container. object. __iter__ ( self$_{)}$ Called by dict . __getitem__() to implement self[key] for dict subclasses when key is not in the dictionary. object. __missing__ ( self , key ) Called to implement deletion of$_{self[key]}$. Same note as for$_{__getitem__()}$ . This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper key values as for the$_{__getitem__()}$ method. object. __delitem__ ( self , key$_{)}$ Called to implement assignment to self[key] . Same note as for __getitem__() . This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper key values as for the __getitem__() method. object. __setitem__ ( self , key , value$_{)}$ The Python Language Reference, Release 3.9.5 if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), IndexError should be raised. For mapping types, if key is missing (not in the container), KeyError should be raised. Note: $_{for}$loops expect that an$_{IndexError}$ will be raised for illegal indexes to allow proper detection of the end of the sequence. The membership test operators ($_{in}$ and$_{not in}$) are normally implemented as an iteration through a container. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be iterable. Chapter 3. Data model 38