LWN.net Logo

Danjou: The definitive guide on how to use static, class or abstract methods in Python

On his blog, Julien Danjou examines static, class, and abstract methods in Python and gives examples of how they might be used. "Doing code reviews is a great way to discover things that people might struggle to comprehend. While proof-reading OpenStack patches recently, I spotted that people were not using correctly the various decorators Python provides for methods. So here's my attempt at providing me a link to send them to in my next code reviews. :-)"
(Log in to post comments)

Danjou: The definitive guide on how to use static, class or abstract methods in Python

Posted Aug 1, 2013 21:14 UTC (Thu) by agforsyth (subscriber, #62822) [Link]

It's a nice guide. The only thing I'd add is that "unbound methods", which he mentions right at the start, go away in Python 3.
Python 3.3.1 (default, May 31 2013, 14:31:29) 
>>> class A:
...  def b(): pass
... 
>>> A.b
<function A.b at 0x10ecad9e0>
Python 2.7.4 (default, May 17 2013, 14:54:24) 
>>> class A(object):
...  def b(): pass
... 
>>> A.b
<unbound method A.b>

Danjou: The definitive guide on how to use static, class or abstract methods in Python

Posted Aug 2, 2013 12:17 UTC (Fri) by acid (subscriber, #13770) [Link]

Nice idea to mention that! I've added a small paragraph about that change in Python 3.

Thank you!

Danjou: The definitive guide on how to use static, class or abstract methods in Python

Posted Aug 2, 2013 6:41 UTC (Fri) by Otus (guest, #67685) [Link]

I imagine part of the reason is that static methods are not static - at least in the sense usually meant. As the guide points out, you can override a static method in a subclass, since when called through an instance object they are not in fact statically bound (as Python isn't statically typed).

Class methods, of course, don't exist in all languages, so those who've learned their OOP with Java, C++ etc. will have had no experience with them.

Danjou: The definitive guide on how to use static, class or abstract methods in Python

Posted Aug 8, 2013 22:01 UTC (Thu) by marcH (subscriber, #57642) [Link]

> static methods are not static - at least in the sense usually meant.

What is the usual?

Danjou: The definitive guide on how to use static, class or abstract methods in Python

Posted Aug 9, 2013 1:26 UTC (Fri) by hummassa (subscriber, #307) [Link]

I think the OP meant that in the "usual" (C++-centric?) sense, "static method" == "non-virtual class method", while in Python static method == virtual class method.

Danjou: The definitive guide on how to use static, class or abstract methods in Python

Posted Aug 15, 2013 18:53 UTC (Thu) by Otus (guest, #67685) [Link]

Yes, I meant statically resolved, without depending on the object's type. My thinking was more Java-centric, than C++ though.

Danjou: The definitive guide on how to use static, class or abstract methods in Python

Posted Aug 2, 2013 15:55 UTC (Fri) by ndk (subscriber, #43509) [Link]

[Trivial] compute_circumference should be compute_area.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds