Trying to come to grips with static methods

Cyril Bazin cyril.bazin at gmail.com
Tue Jul 12 05:08:12 EDT 2005


(sorry, my fingers send the mail by there own ;-)

Im my opinion, class method are used to store some functionalities 
(function) related to a class in the scope of the class.
For example, I often use static methods like that:

class Point:
def __init__(self, x, y):
self.x, self.y = x, y

def fromXML(xmlText):
x, y = functionToParseXMLUsingMinidomForExample(xmlText)
return Point(x, y)
fromXML = staticmethod(fromXML)

Here, it is used to define some kind of second constructor... 

Note that class decorator can simplify the notation, but break the 
compatility with older Python...

Cyril


On 7/12/05, Steven D'Aprano <steve at removethiscyber.com.au> wrote:
> 
> 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.
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050712/c7ca35ab/attachment.html>


More information about the Python-list mailing list