[Tutor] static methods and class methods

Alan Gauld alan.gauld at btinternet.com
Fri Jun 13 03:03:40 CEST 2008

"Kent Johnson" <kent37 at tds.net> wrote in message 
news:1c2a2c590806121724q611456f1yefb8ebe54399b165 at mail.gmail.com...
> 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.

Sorry Kent, I got my header editing a bit mixed up.

>> 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

Yes, but I'm fairkly sure I read an article by guido in a magazine
or maybe the O'Reilly blogsite that they came up with the
classmethod idea after they had already done the staticmethod
stuff so they kept both.

But I may be hallucinating again! :-)

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

Conceptually they are both ways of defining a method that
applies at the class level and could be used to implement
class wide behavior. Thats what I mean. If you want to build
a method to determine how many instances are active at
any time then you could use either a staticmethod or a
classmethod to do it. Most languages only give you one
way. Python, despite its mantra, actually gives 2 ways to
do it in this case.

>> In contrast most of my major projects have used class methods

> 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.

Notice the space. I mean "class methods" in the OOP sense
not specifically classmethods in the Python sense. I have never
found anything in Python that I needed a classmethod rather
than a staticmethod, although if I were writing dynamic AI type
stuff I suspect I could find examples. But in almost any major
project I will have a use for class based methods

One specific example of how I've used classmethods in a
networked client/server app was where we had a GUI client talking
to a server. Both GUI and server used the same class interaces
but the GUI implementation of the interface was just a pass through
to the server. When the GUI messages reached the server a listener
picked the message from a socket, decoded the target class and
invoked a getInstance() class method. The class method extracted
the instance ID from the message. If the object was in memory
(an object pool) it called the requested method on the instance
and returned the response to the GUI. If the instance was not
in memory but an instanceID was provided it fetched the instance
from the database and returned the called method response. If no
ID was given it created a new instance, saved it to the database
and then called the method on the new instance.

If the message was a query then it was performed by another
class method which just ran a SELECT on the underlying database

Its pretty obvious that the class methods here were non trivial.
We also had garbage collection and pool management class
methods too. We had about 100 data classes like this with
the core functionality in a super DBClass' class methods which
were in turn called by each subclass' class methods along
with their individual attribute wrappers. To add to the interest
the data was stored in multiple databases on different boxes
around the globe, so the class methods also had to figure out
which database instance to talk to when fetching or writing

Needless to say this was before the days or Object databases
and ORMs! But the final system managed millions of objects
with thousands of users in around a dozen countries, and did
so for many years. (And all in C++ :-)

Hope that makes sense!

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list