on undefined attribute fetch [obj namedef __getattribute__(selfname)on all attribute fetch [obj namedef __setattr__(selfnamevalue)on all attribute assignment [obj name=valuedef __delattr__(selfname)on all attribute deletion [del obj namein all of theseself is the subject instance object as usualname is the string name of the attribute being accessedand value is the object being assigned to the attribute the two get methods normally return an attribute' valueand the other two return nothing (noneall can raise exceptions to signal prohibited access for exampleto catch every attribute fetchwe can use either of the first two previous methodsand to catch every attribute assignment we can use the third the following uses __getattr__ and works portably on both python and xnot requiring newstyle object derivation in xclass catcherdef __getattr__(selfname)print('get%snamedef __setattr__(selfnamevalue)print('set% % (namevalue) catcher( job pay pay prints "getjobprints "getpayprints "setpay using __getattribute__ works exactly the same in this specific casebut requires object derivation in (only)and has subtle looping potentialwhich we'll take up in the next sectionclass catcher(object)def __getattribute__(selfname)print('get%snamerest unchanged need (objectin only works same as getattr here but prone to loops on general such coding structure can be used to implement the delegation design pattern we met earlierin because all attributes are routed to our interception methods genericallywe can validate and pass them along to embeddedmanaged objects the following class (borrowed from )for exampletraces every attribute fetch made to another object passed to the wrapper (proxyclassclass wrapperdef __init__(selfobject)self wrapped object def __getattr__(selfattrname)print('traceattrnamereturn getattr(self wrappedattrnamex wrapper([ ] append( print( wrappedsave object trace fetch delegate fetch prints "traceappendprints "[ ]there is no such analog for properties and descriptorsshort of coding accessors for every possible attribute in every possibly wrapped object on the other handwhen __getattr__ and __getattribute__