Making a decorator a staticmethod

Zac Burns zac256 at gmail.com
Thu Jan 8 20:18:37 CET 2009


I've read the "Making staticmethod objects callable?" thread now, and
would have to disagree that all the use cases are strange as stated at
http://www.python.org/dev/summary/2006-03-01_2006-03-15/#making-staticmethod-objects-callable

In my use case (not the example below) the decorator returns a
function of the form def f(self, *args, **kwargs) which makes use of
attributes on the instance self. So, it only makes sense to use the
staticmethod in the class and in the baseclass. Making this decorator
a module level function doesn't make sense here.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 8, 2009 at 10:54 AM, Zac Burns <zac256 at gmail.com> wrote:
> I have a decorator in a class to be used by that class and by inheriting classes
>
> ######
> class C(object):
>        @staticmethod # With  this line enabled or disabled usage in either C
> or D will be broken. To see that D works remember to remove usage in C
>        def decorateTest(func):
>                def newFunc(*args, **kwargs):
>                        print args, kwargs
>                        return func(*args, **kwargs)
>                return newFunc
>
>        @decorateTest
>        def testDecorated(self):
>                return None
> class D(C):
>        @C.decorateTest
>        def test2(self):
>                return None
> ######
>
> The exception that I get when using it as a staticmethod and try to
> use it in the baseclass is "TypeError: 'staticmethod' object is not
> callable".
> When it is not staticmethod the exception I get in the extension class
> is is "TypeError: unbound method decorateTest() must be called with C
> instance as first argument (got function instance instead)"
>
> Python version is 2.5.1
>
> --
> Zachary Burns
> (407)590-4814
> Aim - Zac256FL
> Production Engineer (Digital Overlord)
> Zindagi Games
>



More information about the Python-list mailing list