What is a class method?

Denis Gomes denisg640 at gmail.com
Mon Aug 23 18:31:11 CEST 2010


John,

   I agree with you and I also think the definition given on the official
python site is somewhat confusing, at least for an engineer like myself.
But I'll take a stab at explaning it using what I know thus far.

  I think to understand what a class method is you have to first understand
what a class variable is.  Take this code snippet for example.

class foo(object):
     x=10
     def __init__(self):
          self.x=20

In this example if you create an instance of foo and call it f.  You can
access the instance attribute x by using f.x or you can access the class
variable using the notation foo.x.  These two will give you two different
results.  Now lets do something a bit more interesting.
Say you have the following snippet.

class foo(object):
     x=0
     def __init__(self):
          self.x=10
          foo.x+=1

>>>f=foo()
>>>g=goo()
>>>f.x
10
>>>g.x
10
>>>foo.x
2

What happened here is that the class variable foo.x is used to keep a count
of the total number of instances created.  So we see that a class variable
can be looked at as what "connects" the two instances in a way, so that data
can be shared between instances of the same class.  This defintion may very
well only apply to this case, but I think the mechanics is fundamentally the
same.
Keeping this in mind, lets make the jump to class methods.  When an
instance of a class is created, the methods are just functions that "work
on" the attributes (the variables).  Similarly, a class method is a method
that works on a class variable.  For example,

class foo(object):
     x=10
     def __init__(self):
          self.x=20
     @classmethod
     def change(self):
           self.x=15

>>>f=foo()
>>>f.x
20
>>>foo.x
10
>>>f.change()
>>>f.x
20
>>>foo.x
15

So this is my explanation for what a classmethod is.  Hope it helps.  Good
luck.

Denis

On Mon, Aug 23, 2010 at 2:24 AM, John Nagle <nagle at animats.com> wrote:

> On 8/22/2010 9:16 PM, James Mills wrote:
>
>> On Mon, Aug 23, 2010 at 1:53 PM, Paulo da Silva
>>
>> <psdasilva.nospam at netcabonospam.pt>  wrote:
>>
>>> I did it before posting ...
>>>
>>> The "explanation" is not very clear. It is more like "how to use it".
>>>
>>
>> Without going into the semantics of languages basically the
>> differences are quite clear:
>>
>> @classmethod is a decorator that warps a function with
>> passes the class as it's first argument.
>>
>> @staticmethod (much like C++/Java) is also a decorator that
>> wraps a function but does not pass a class or instance as
>> it's first argument.
>>
>
>    That reads like something the C++ standards revision committee
> would dream up as they add unnecessary template gimmicks to the
> language.
>
>                                John Nagle
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100823/a987962d/attachment.html>


More information about the Python-list mailing list