diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index f018b0e..315c4e1 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -279,10 +279,17 @@ they are invoked from an object or a class. In pure python, it works like this:: class Function(object): - . . . + def __init__(self, f): + self.f = f + def __get__(self, obj, objtype=None): "Simulate func_descr_get() in Objects/funcobject.c" - return types.MethodType(self, obj, objtype) + if obj is Null: + # Get from a class returns a function + return self.f + else: + # Get from an instance returns a bound method + return types.MethodType(self.f, obj) Running the interpreter shows how the function descriptor works in practice:: @@ -293,8 +300,8 @@ Running the interpreter shows how the function descriptor works in practice:: >>> d = D() >>> D.__dict__['f'] # Stored internally as a function - >>> D.f # Get from a class becomes an unbound method - + >>> D.f # Get from a class becomes a function as well + >>> d.f # Get from an instance becomes a bound method >