Implementing bound method using descriptor

Question:

My question is related to bound method object, more specifically I am trying to imitate the same behavior that functions (which are essentially descriptors) present.

Let me first put here my understanding of how class_instance.function() works

class D(object):
    def dfunc(self):
        ...
 
d=D()
d.dfunc() # will call D.__dict__['dfunc'].__get__(d,D)()

# gives a bound method which can be used directly
a=D.__dict__['dfunc'].__get__(d,D) 
a() # works as d.D()

Does Python call D.__dict__['dfunc'] (d), in case when type(dfunc) is function and type(d).__dict__['dfunc'].__get__(d, type(d)) if x is a class instance?

Now coming to how I am trying to create a bound object using closure:

class E(object):
    def __get__(self,obj,cls):
    
        def returned(*args):
            print(obj.__dict__)
        return returned

class F(object):
    e = E()
    y = 9

f = F()
f.fvar = 9 # putting something in instances __dict__
a = f.e # will call the __get__ function of F and return a closure
a() # works and produces instance specific result -> {'fvar': 9}, just like a bound method

Now the main question,
when I did a = D.__dict__['dfunc'].__get__(d,D) type(a) was bound method, but when I implement it type(a) is function; is there a way to return bound object from user-defined descriptor?

Is there any better way to implement a function like descriptor?
Please correct me if I have gotten something fundamentally incorrect in my head.

Asked By: Dev Maha

||

Answers:

If you want to return a method instead, you can create one from the function object together with the arguments passed to __get__:

import types

class E(object):
    def __get__(self,obj,cls):

        def returned(*args):
            print(obj.__dict__)
        return types.MethodType(returned, obj, cls)

However, it’s not totally clear what this gains you. If just returning a function already works, why do you need it to be a “real” method?

Answered By: BrenBarn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.