[BangPypers] Classmethod, Decorators and staticmethod

Praveen Kumar praveen.python.plone at gmail.com
Fri Mar 6 07:01:42 CET 2009


Hi Vijay,

If you would have googled then you get number of links and may get more idea
about that. You should try yourself first. BTW

classmethod
-----------------
classmethod(function)
Returns a class method for function.

A class method receives the class as implicit first argument, just like an
instance method receives the instance. To declare a class method, use this
idiom:

class C:
  @classmethod
  def f(cls, arg1, arg2, ...): ...

The @classmethod form is a function decorator
now you will be thinking about function decorator
Decorators were introduced to the Python world as part of the 2.4 release.
They have since become increasingly importantin, especially for maintaining
and extending Python programs. In order to best grasp the purpose and
beneficiality of Python's decorators, it is important to understand what
happens when one imports modules. (to know more about
http://python.about.com/od/gettingstarted/ss/begpydecorators.htm)

It can be called either on the class (such as C.f()) or on an instance (such
as C().f()). The instance is ignored except for its class. If a class method
is called for a derived class, the derived class object is passed as the
implied first argument.

Class methods are different than C++ or Java static methods.
staticmethod
--------------------
staticmethod(function)
Returns a static method for function.

A static method does not receive an implicit first argument. To declare a
static method, use this idiom:

class C:
  @staticmethod
  def f(arg1, arg2, ...): ...

The @staticmethod form is a function decorator

It can be called either on the class (such as C.f()) or on an instance (such
as C().f()). The instance is ignored except for its class.

Static methods in Python are similar to those found in Java or C++.

Thanks

Praveen Kumar

+91 9739854134

Bangalore
On Fri, Mar 6, 2009 at 9:56 AM, Roshan Mathews <rmathews at gmail.com> wrote:

> On Fri, Mar 6, 2009 at 9:48 AM, Venkatraman S <venkat83 at gmail.com> wrote:
> > Damn! dont you know that *real* world is made ONLY of 'objects' :P
> >
> :)
>
> No, but I was serious about wanting to know about the course.
>
> ~roshan
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20090306/ddee3216/attachment.htm>


More information about the BangPypers mailing list