Questions about the _ _ get__ method of function in Python

when you look at descriptor, you can see that an ordinary function is actually an instance of a class, function class, just because the definition has a special _ _ get__ method, so there are all kinds of function, bound method and so on.

so since an instance of the function class is generated when def xxx , isn"t another instance of function generated when def _ get__ () in this instance? Isn"t this a recursive definition?

Mar.23,2021

will defining the function def xxx in the function body ( def top (): ) result in a function instance? The answer is yes. From the result of translation into opcode, the MAKE_FUNCITON instruction is executed in the function body, which applies for a memory space and sets the stack information and variable space in which it runs, which is equivalent to an instance of new . Every time the top () function is called, because def xxx is defined in the function, the MAKE_FUNCITON instruction is still executed, that is, every time top is called, the xxx = new Funciton (), in it will be a different function object.

the _ get__ () mentioned in the title is a special built-in method. If you print (type (top.__get__) , you can see that it is not an instance of the function, but < class' method-wrapper' > . It should not work to define the _ _ get__ () method within the function to override it, so there is no problem of recursive definition.

this question feels like a good question, but there is no one to discuss it.


I guess you may want to ask: when a.b , b.roomgegetrequests _ () will be called, so will b.roomgetrequests _ also call _ _ get__.__get__ ?

Menu