What is a class method?

Ian Kelly ian.g.kelly at gmail.com
Mon Aug 23 01:16:45 EDT 2010


On Sun, Aug 22, 2010 at 9:53 PM, Paulo da Silva
<psdasilva.nospam at netcabonospam.pt> wrote:
> Em 23-08-2010 04:30, James Mills escreveu:
>> On Mon, Aug 23, 2010 at 12:49 PM, Paulo da Silva
>> <psdasilva.nospam at netcabonospam.pt> wrote:
>>> I understand the concept of a static method.
>>> However I don't know what is a class method.
>>> Would anybody pls. explain me?
>>
>> Please read this first:
>> http://docs.python.org/library/functions.html#classmethod
>>
>> Then ask us questions :)
>
> I did it before posting ...
> The "explanation" is not very clear. It is more like "how to use it".

Consider this:

class A(object):
    @staticmethod
    def new():
        return A()

class B(A):
    pass

versus this:

class C(object):
    @classmethod
    def new(cls):
        return cls()

class D(C):
    pass

B.new() will return a new instance of A, not B.  D.new() will return a
new instance of D.

Does this answer your question?



More information about the Python-list mailing list