Trying to come to grips with static methods

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Jul 12 04:42:38 EDT 2005


I've been doing a lot of reading about static methods in Python, and I'm
not exactly sure what they are useful for or why they were introduced.

Here is a typical description of them, this one from Guido:

"The new descriptor API makes it possible to add static methods and class
methods. Static methods are easy to describe: they behave pretty much like
static methods in C++ or Java."
http://www.python.org/2.2.3/descrintro.html

Great. So I have learn an entire new language to understand static
methods. Perhaps not -- hence this cry for help.

As near as I can see it, static methods are object methods that act just
like functions. Er. I always thought that object methods *were* functions,
except they had some runtime magic that passed the object itself as the
first argument.

>From Guido's example:

>>> class C:
...     def foo(x, y):
...             print "staticmethod", x, y
...     foo = staticmethod(foo)
...
>>> C.foo(1, 2)
staticmethod 1 2
>>> c = C()
>>> c.foo(1, 2)
staticmethod 1 2

So I compare with an ordinary class function, er, method:

>>> class D:
...     def foo(self, x, y):
...             print "method", x, y
...
>>> D.foo(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method foo() must be called with D instance as first 
argument (got int instance instead)

Okay, that is to be expected. Actually, I expected an exception that I
hadn't passed enough arguments (2 arguments when foo expects 3), but in
hindsight it is obvious enough.

First point of confusion. In the above exception, foo is called an unbound
method. But type(D.foo) returns <type 'instancemethod'> even though foo is
being access through the class, not an instance. And type(D().foo) returns
the same.

Can I assume that in Python "unbound method" is just another way of saying
"a method of a class that expects to be called via an instance"?



I next tried this:

>>> D.foo(D(), 1, 2)
method 1 2
>>> D().foo(1, 2)
method 1 2

Clear as mud. An ordinary method called from an instance is the same as a
static method called from anywhere, provided you don't -- or rather, can't
-- try to access self from the static method.

When would you use a static method instead of an ordinary method? It has
been suggested that you might use it for functions that don't need to
access self. But that doesn't seem very convincing to me, because there is
already a perfectly good idiom for that:

>>> class E:
...     def foo():  # returns calculated value
...             return 1
...     foo = staticmethod(foo)
...     def bar(self):
...             return 1  # just ignore the value of self
...
>>> E.foo()
1
>>> e = E()
>>> e.bar()
1

What are some usage cases for using Class.StaticMethod() instead of
instance.method()? Everything I've read seems to just assume that the
benefits of static methods are so obvious that they don't need explaining.
Unfortunately, I haven't come from a background in OO and I'm easily
confused, hence this post.


-- 
Steven.





More information about the Python-list mailing list