[Tutor] Class vs. Static Methods

Alan G alan.gauld at freenet.co.uk
Wed Jun 22 01:03:00 CEST 2005


> Sorry for the elementary question: I was wondering if someone could
> explain the difference to me between class and static methods.
Coming
> from other languages, I'm used to static methods, but not "class
> methods". Thanks.

There probably is a deep and subtle difference in Python but to
all intents and purposes they are the same thing.

class method is the original and logically correct name and has
been around in Smalltalk, Objective C, Lisp and most early OOP
languages for a long time. It means a method of the class itself
rather than of an instance and is typically used to perform
an operation on the entire class - ie all the existing instances.

The term 'static' comes from C++ where it refers to the fact
that the code for static methods lives on the heap rather
than the stack and so is persistent and the variables
effectively shared - this echos C's use of 'static' variables.
Java and Object Pascal copied the name from C++ and now Python
seems to have adopted both names just to accomodate all tastes!

Alan G.



More information about the Tutor mailing list