Im my opinion, class method are used to store some "functions" related to a class in the scope of the class.<br>
<br>
For example, I often use static methods like that:<br>
class Foo:<br>
     <br>
<br><br><div><span class="gmail_quote">On 7/12/05, <b class="gmail_sendername">Steven D'Aprano</b> <<a href="mailto:steve@removethiscyber.com.au">steve@removethiscyber.com.au</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I've been doing a lot of reading about static methods in Python, and I'm<br>not exactly sure what they are useful for or why they were introduced.<br><br>Here is a typical description of them, this one from Guido:<br><br>
"The new descriptor API makes it possible to add static methods and class<br>methods. Static methods are easy to describe: they behave pretty much like<br>static methods in C++ or Java."<br><a href="http://www.python.org/2.2.3/descrintro.html">
http://www.python.org/2.2.3/descrintro.html</a><br><br>Great. So I have learn an entire new language to understand static<br>methods. Perhaps not -- hence this cry for help.<br><br>As near as I can see it, static methods are object methods that act just
<br>like functions. Er. I always thought that object methods *were* functions,<br>except they had some runtime magic that passed the object itself as the<br>first argument.<br><br>>From Guido's example:<br><br>>>> class C:
<br>...     def foo(x, y):<br>...             print "staticmethod", x, y<br>...     foo = staticmethod(foo)<br>...<br>>>> C.foo(1, 2)<br>staticmethod 1 2<br>>>> c = C()<br>>>> c.foo(1, 2)
<br>staticmethod 1 2<br><br>So I compare with an ordinary class function, er, method:<br><br>>>> class D:<br>...     def foo(self, x, y):<br>...             print "method", x, y<br>...<br>>>> D.foo
(1, 2)<br>Traceback (most recent call last):<br>  File "<stdin>", line 1, in ?<br>TypeError: unbound method foo() must be called with D instance as first<br>argument (got int instance instead)<br><br>Okay, that is to be expected. Actually, I expected an exception that I
<br>hadn't passed enough arguments (2 arguments when foo expects 3), but in<br>hindsight it is obvious enough.<br><br>First point of confusion. In the above exception, foo is called an unbound<br>method. But type(D.foo) returns <type 'instancemethod'> even though foo is
<br>being access through the class, not an instance. And type(D().foo) returns<br>the same.<br><br>Can I assume that in Python "unbound method" is just another way of saying<br>"a method of a class that expects to be called via an instance"?
<br><br><br><br>I next tried this:<br><br>>>> D.foo(D(), 1, 2)<br>method 1 2<br>>>> D().foo(1, 2)<br>method 1 2<br><br>Clear as mud. An ordinary method called from an instance is the same as a<br>static method called from anywhere, provided you don't -- or rather, can't
<br>-- try to access self from the static method.<br><br>When would you use a static method instead of an ordinary method? It has<br>been suggested that you might use it for functions that don't need to<br>access self. But that doesn't seem very convincing to me, because there is
<br>already a perfectly good idiom for that:<br><br>>>> class E:<br>...     def foo():  # returns calculated value<br>...             return 1<br>...     foo = staticmethod(foo)<br>...     def bar(self):<br>...             return 1  # just ignore the value of self
<br>...<br>>>> E.foo()<br>1<br>>>> e = E()<br>>>> e.bar()<br>1<br><br>What are some usage cases for using Class.StaticMethod() instead of<br>instance.method()? Everything I've read seems to just assume that the
<br>benefits of static methods are so obvious that they don't need explaining.<br>Unfortunately, I haven't come from a background in OO and I'm easily<br>confused, hence this post.<br><br><br>--<br>Steven.<br><br><br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>