[Python-3000] The case for unbound methods?
Anthony Tolle
artomegus at gmail.com
Mon Mar 10 01:07:54 CET 2008
On Sun, Mar 9, 2008 at 7:53 PM, Anthony Tolle <artomegus at gmail.com> wrote:
> On Sun, Mar 9, 2008 at 6:31 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> >
> > No, a wrapper can't distinguish between a plain function and
> > an unbound method this way, because it gets called before the
> > function is put into a class. So it's always wrapping a plain
> > function, not an unbound method object.
> >
>
> Witness:
>
> >>> def wraptest(func):
> ... print "I'm wrapping an instance of ", type(func)
> ... return func
> ...
> >>> class C(object):
> ... @wraptest
> ... @staticmethod
> ... def s():
> ... pass
> ... @wraptest
> ... @classmethod
> ... def c():
> ... pass
> ... @wraptest
> ... def i():
> ... pass
> ...
> I'm wrapping an instance of <type 'staticmethod'>
> I'm wrapping an instance of <type 'classmethod'>
> I'm wrapping an instance of <type 'function'>
> >>>
>
Sorry about replying to myself, but I should clarify: That particular
point in my post was about checking the type of wrapped object
*before* calling __get__ on it, to see whether or not I'm wrapping a
static method. The theory is, if I don't see 'staticmethod' or
'classmethod', then simple deduction tells me I'm going to get either
a bound method or an unbound method. Whether it is bound or unbound
won't be known until I check what is returned by __get__.
Of course, the other point was that this would only work on the first
wrapper around @staticmethod or @classmethod. I wasn't offering it as
a real solution.
More information about the Python-3000
mailing list