intercepts explicit indexing as well as slicing slice expressions trigger __getitem__ with slice object containing boundsboth for built-in types and user-defined classesso slicing is automatic in our classfrom contains import iters iters('spam' [ get[ ]:'sindexing __getitem__( 'spam'[ :'pam'spam'[slice( none)'pamslice syntax [ :get[slice( nonenone)]:'pamx[:- get[slice(none- none)]:'spa__getitem__(slice)slice object list(xand iteration tooiter=next:next:next:next:next:[' '' '' '' 'in more realistic iteration use cases that are not sequence-orientedthoughthe __iter__ method may be easier to write since it must not manage an integer indexand __contains__ allows for membership optimization as special case attribute access__getattr__ and __setattr__ in pythonclasses can also intercept basic attribute access ( qualificationwhen needed or useful specificallyfor an object created from classthe dot operator expression object attribute can be implemented by your code toofor referenceassignmentand deletion contexts we saw limited example in this category in but will review and expand on the topic here attribute reference the __getattr__ method intercepts attribute references it' called with the attribute name as string whenever you try to qualify an instance with an undefined (nonexistentattribute name it is not called if python can find the attribute using its inheritance tree search procedure because of its behavior__getattr__ is useful as hook for responding to attribute requests in generic fashion it' commonly used to delegate calls to embedded (or "wrapped"objects from proxy controller object--of the sort introduced in ' introduction to delegation this method can also be used to adapt classes to an interfaceor add accessors for data attributes after the fact--logic in method that validates or computes an attribute after it' already being used with simple dot notation attribute access__getattr__ and __setattr__