Dynamic class methods misunderstanding

Alex Martelli aleaxit at yahoo.com
Mon Jan 31 12:56:38 CET 2005

Christos TZOTZIOY Georgiou <tzot at sil-tec.gr> wrote:
> >> > class Test:
> >> >     def __init__(self, method):
> >> >         self.m = new.instancemethod(method, self, Test)
> >self.m = method.__get__(self, Test)
> Almost true; not all builtin functions are descriptors though.
> AttributeError: 'builtin_function_or_method' object has no attribute
> '__get__'
> I didn't run an extensive test, but it's possible that all builtin
> functions are not descriptors.

Indeed, many callables (builtin-functions, types, ...) are not
descriptors, so you can't call __get__ on them.  But then, many
descriptors (statimethods, classmethods, ...) are not callable, so you
can't pass them as the first argument to instancemethod.  Not having any
indication of what the author of class Test means to pass in as
"method", it's not clear whether Test(divmod) or Test(staticmethod(foo))
is more likely to be of interest to them as a use case.  If ``method''
is a function, which is both a descriptor AND callable, either approach
works; otherwise, one has to pick an approach, or try both approaches
with a try/except.


More information about the Python-list mailing list