Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>When and how are static methods suppose to be used in python?</p> </blockquote> <p>The glib answer is: Not very often.</p> <p>The even glibber but not quite as useless answer is: When they make your code more readable.</p> <hr> <p>First, let's take a detour to <a href="http://docs.python.org/2/library/functions.html#staticmethod" rel="noreferrer">the docs</a>:</p> <blockquote> <p>Static methods in Python are similar to those found in Java or C++. Also see <code>classmethod()</code> for a variant that is useful for creating alternate class constructors.</p> </blockquote> <p>So, when you need a static method in C++, you need a static method in Python, right?</p> <p>Well, no.</p> <p>In Java, there are no functions, just methods, so you end up creating pseudo-classes that are just bundles of static methods. The way to do the same thing in Python is to just use free functions. </p> <p>That's pretty obvious. However, it's good Java style to look as hard as possible for an appropriate class to wedge a function into, so you can avoid writing those pseudo-classes, while doing the same thing is bad Python style—again, use free functions—and this is much less obvious.</p> <p>C++ doesn't have the same limitation as Java, but many C++ styles are pretty similar anyway. (On the other hand, if you're a "Modern C++" programmer who's internalized the "free functions are part of a class's interface" idiom, your instincts for "where are static methods useful" are probably pretty decent for Python.)</p> <hr> <p>But if you're coming at this from first principles, rather than from another language, there's a simpler way to look at things:</p> <p>A <code>@staticmethod</code> is basically just a global function. If you have a function <code>foo_module.bar()</code> that would be more readable for some reason if it were spelled as <code>foo_module.BazClass.bar()</code>, make it a <code>@staticmethod</code>. If not, don't. That's really all there is to it. The only problem is building up your instincts for what's more readable to an idiomatic Python programmer.</p> <p>And of course use a <code>@classmethod</code> when you need access to the class, but not the instance—alternate constructors are the paradigm case for that, as the docs imply. Although you often <em>can</em> simulate a <code>@classmethod</code> with a <code>@staticmethod</code> just by explicitly referencing the class (especially when you don't have much subclassing), you shouldn't.</p> <hr> <p>Finally, getting to your specific question:</p> <p>If the only reason clients ever need to look up data by ID is to construct an <code>Entity</code>, that sounds like an implementation detail you shouldn't be exposing, and it also makes client code more complex. Just use a constructor. If you don't want to modify your <code>__init__</code> (and you're right that there are good reasons you might not want to), use a <code>@classmethod</code> as an alternate constructor: <code>Entity.from_id(id_number, db_connection)</code>.</p> <p>On the other hand, if that lookup is something that's inherently useful to clients in other cases that have nothing to do with <code>Entity</code> construction, it seems like this has nothing to do with the <code>Entity</code> class (or at least no more than anything else in the same module). So, just make it a free function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload