[Tutor] static methods and class methods

Kent Johnson kent37 at tds.net
Fri Jun 13 02:24:38 CEST 2008


On Thu, Jun 12, 2008 at 7:48 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> "Kent Johnson" <kent37 at tds.net> wrote>
>
>>  I am still unsure of the difference of static and class methods.

No, that wasn't me, it was Christopher Spears.
>
> You are not alone, it is confusing!
>
>> A class method receives the class it was called on as the first
>> argument. This can be useful with subclasses. A staticmethod doesn't
>> get a a class or instance argument. It is just a way to put a plain
>> function into the scope of a class.
>
> And that's the definition of the difference in Python.

Yes, that is what I said :-)

> Python started off implementing "static methods" then later
> developed the sligtly more powerfull and flexible "class methods" and
> rather than lose backward compatibility called them classmethod.

Are you sure? They were both introduced in Python 2.2 with new-style classes:
http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html

PEP 252 has an entire section on Static methods and class methods:
http://www.python.org/dev/peps/pep-0252/

> So in Python we have two ways of doing more or less the same
> (conceptual) thing.

?? But they are not the same thing.

> In contrast most of my major projects have used class methods
> in some way or other. Often in conjunction with factory classes or
> as factory methods, but also as a persistence mechanism, a
> streaming mechanism or as a database access technique.

I would be interested in an example if you care to share. I have used,
or can imagine using, static methods that way but not class methods.

Kent


More information about the Tutor mailing list