[Tutor] what is @classmethod and @staticmethod ??

Andreas Kostyrka andreas at kostyrka.org
Sat Mar 22 10:08:43 CET 2008


Well, it's classmethod/staticmethod in truth, @ is the decorator
operator:

def testdec(func):
    return {"funcobj": func}

class Abc(object):
    @testdec
    def method():
        pass

assert isinstance(Abc.method, dict)

Basically as you can see above, @X before a function definition takes
the function, applies X, and use the result instead.

Now, naive Python level implementations of classmethod and staticmethod
would be (untested, all typed in the mailer):

def classmethod(func):
    def wrapper(self, *args, **kw):
        return func(self.__class__, *args, **kw)
    return wrapper

def staticmethod(func):
    def wrapper(self, *args, **kw):
        return func(*args, **kw)
    return wrapper

Andreas

Am Freitag, den 21.03.2008, 20:08 -0700 schrieb maser:
> Hi
> 
> I couldn't find a good resource explaining what
> @classmethod and @staticmethod are in python and when,
> how these could be used.
> 
> If someone could explain what these are, or point me
> to resources that may help, it is appreciated.
> 
> Thanks
> iyer
> 
> 
>       ____________________________________________________________________________________
> Be a better friend, newshound, and 
> know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080322/b6c9def3/attachment-0001.pgp 


More information about the Tutor mailing list