Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yep, using the <a href="https://docs.python.org/2/library/functions.html#staticmethod" rel="noreferrer" title="staticmethod">staticmethod</a> decorator</p> <pre><code>class MyClass(object): @staticmethod def the_static_method(x): print x MyClass.the_static_method(2) # outputs 2 </code></pre> <p>Note that some code might use the old method of defining a static method, using <code>staticmethod</code> as a function rather than a decorator. This should only be used if you have to support ancient versions of Python (2.2 and 2.3)</p> <pre><code>class MyClass(object): def the_static_method(x): print x the_static_method = staticmethod(the_static_method) MyClass.the_static_method(2) # outputs 2 </code></pre> <p>This is entirely identical to the first example (using <code>@staticmethod</code>), just not using the nice decorator syntax</p> <p>Finally, use <a href="https://docs.python.org/2/library/functions.html#staticmethod" rel="noreferrer" title="staticmethod"><code>staticmethod()</code></a> sparingly! There are very few situations where static-methods are necessary in Python, and I've seen them used many times where a separate "top-level" function would have been clearer.</p> <hr> <p><a href="https://docs.python.org/2/library/functions.html#staticmethod" rel="noreferrer" title="staticmethod">The following is verbatim from the documentation:</a>:</p> <blockquote> <p>A static method does not receive an implicit first argument. To declare a static method, use this idiom:</p> <pre><code>class C: @staticmethod def f(arg1, arg2, ...): ... </code></pre> <p>The @staticmethod form is a function <a href="https://docs.python.org/2/glossary.html#term-decorator" rel="noreferrer" title="term-decorator"><em>decorator</em></a> – see the description of function definitions in <a href="https://docs.python.org/2/reference/compound_stmts.html#function" rel="noreferrer" title="Function definitions"><em>Function definitions</em></a> for details.</p> <p>It can be called either on the class (such as <code>C.f()</code>) or on an instance (such as <code>C().f()</code>). The instance is ignored except for its class.</p> <p>Static methods in Python are similar to those found in Java or C++. For a more advanced concept, see <a href="https://docs.python.org/2/library/functions.html#classmethod" rel="noreferrer" title="classmethod"><code>classmethod()</code></a>.</p> <p>For more information on static methods, consult the documentation on the standard type hierarchy in <a href="https://docs.python.org/2/reference/datamodel.html#types" rel="noreferrer" title="types"><em>The standard type hierarchy</em></a>.</p> <p>New in version 2.2.</p> <p>Changed in version 2.4: Function decorator syntax added.</p> </blockquote>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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