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

maser maseriyer at yahoo.com
Sun Mar 23 18:49:41 CET 2008


Thanks, Andreas. Why do we need to use classmethod/
staticmethod and where do we need to use them ?

thanks
iyer

--- Andreas Kostyrka <andreas at kostyrka.org> wrote:

> 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
> 



      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


More information about the Tutor mailing list