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

Andreas Kostyrka andreas at kostyrka.org
Sun Mar 23 21:25:06 CET 2008


One usual usage for classmethods are "alternate" constructors.

Andreas


Am Sonntag, den 23.03.2008, 10:49 -0700 schrieb maser:
> 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
-------------- 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/20080323/73725862/attachment.pgp 


More information about the Tutor mailing list