[Tutor] what is @classmethod and @staticmethod ??
Alan Gauld
alan.gauld at btinternet.com
Mon Mar 24 01:22:01 CET 2008
"maser" <maseriyer at yahoo.com> wrote
> Thanks, Andreas. Why do we need to use classmethod/
> staticmethod and where do we need to use them ?
Although there are minor technical details where they differ
static methods and class methods are nearly identical. The
names come from two programming traditions with different
names for the same thing. Smalltalk and Lisp etc use class
method. Delphi, C++ and Javba use static method.
In terms of usage they are both the same and have two basic
purposes:
1) As per my earlier response and example to do something
to the entire class of objects, either select one instance using
some kind of search mechanism - database queries are often
coded in class methods, or a count of all instances (perhaps
with filters which is similar to a database sel;ect) or it can
be conversions of one type/class to another. And this can
in turn be a kind of factory method or pseudo constructor.
2) class methods can also be used to provide generic
collections of functions, perhaps with some shared
data/variables. Booch called these "Class Utilities" in his
book. They could for example provide a pseudo object
interface to legacy code or provide namespace protection
for a commonly used data name.
The second form can usually be provided with functions
in a module in Python but thats not the case in some
other languages.
As a practical example of the former consider a client/server
application where the client sends requests to the server
specifying the class and instance and method of the target
object. The interprocess message handler can delegate the
message to a class method to look up the instance and
in turn call the actual method of the returned instance.
This is much lighter weight than using a full blown Object
Request Broker but provides a simar level of decoupling
and ease of use in the client code. I've built several
systems using this pattern and the RPC code hardly
changes.
Another example is where you need to produce highly
dynamic object systems with lots of introspection and
varying object structures. This is rarely needed in Python
because the built in introspection is so good but in languages
like C++ it may be the easiest way of creating new "classes"
(not objects) in real time (although in truth these "classes" are
restricted to new data members and accessor methods)
One practical case using this was a hospital diagniosis
system where doctors needed to be able to introduce
new subclasses of disease/diagnosis and have these
classes merge seamlessly with the base hard coded
disease classes. Static methods were used to maintain
dictionaries of dictionaries representing the sub classes
and their features. These could then be passed to the
instances as required and the instances performed a
similar kind of attribute access to that used by Python.
Its probably worth saying that these kinds of uses are
powerful when needed but they are not by any means
needed in every program. Although once you use them
you tend to find more uses for them than you previously
expected! :-)
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list