Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Maybe a bit of example code will help: Notice the difference in the call signatures of <code>foo</code>, <code>class_foo</code> and <code>static_foo</code>:</p> <pre><code>class A(object): def foo(self,x): print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): print "executing class_foo(%s,%s)"%(cls,x) @staticmethod def static_foo(x): print "executing static_foo(%s)"%x a=A() </code></pre> <p>Below is the usual way an object instance calls a method. The object instance, <code>a</code>, is implicitly passed as the first argument.</p> <pre><code>a.foo(1) # executing foo(&lt;__main__.A object at 0xb7dbef0c&gt;,1) </code></pre> <hr> <p><strong>With classmethods</strong>, the class of the object instance is implicitly passed as the first argument instead of <code>self</code>.</p> <pre><code>a.class_foo(1) # executing class_foo(&lt;class '__main__.A'&gt;,1) </code></pre> <p>You can also call <code>class_foo</code> using the class. In fact, if you define something to be a classmethod, it is probably because you intend to call it from the class rather than from a class instance. <code>A.foo(1)</code> would have raised a TypeError, but <code>A.class_foo(1)</code> works just fine:</p> <pre><code>A.class_foo(1) # executing class_foo(&lt;class '__main__.A'&gt;,1) </code></pre> <p>One use people have found for class methods is to create <a href="https://stackoverflow.com/a/1950927/190597">inheritable alternative constructors</a>.</p> <hr> <p><strong>With staticmethods</strong>, neither <code>self</code> (the object instance) nor <code>cls</code> (the class) is implicitly passed as the first argument. They behave like plain functions except that you can call them from an instance or the class:</p> <pre><code>a.static_foo(1) # executing static_foo(1) A.static_foo('hi') # executing static_foo(hi) </code></pre> <p>Staticmethods are used to group functions which have some logical connection with a class to the class.</p> <hr> <p><code>foo</code> is just a function, but when you call <code>a.foo</code> you don't just get the function, you get a "partially applied" version of the function with the object instance <code>a</code> bound as the first argument to the function. <code>foo</code> expects 2 arguments, while <code>a.foo</code> only expects 1 argument.</p> <p><code>a</code> is bound to <code>foo</code>. That is what is meant by the term "bound" below:</p> <pre><code>print(a.foo) # &lt;bound method A.foo of &lt;__main__.A object at 0xb7d52f0c&gt;&gt; </code></pre> <p>With <code>a.class_foo</code>, <code>a</code> is not bound to <code>class_foo</code>, rather the class <code>A</code> is bound to <code>class_foo</code>.</p> <pre><code>print(a.class_foo) # &lt;bound method type.class_foo of &lt;class '__main__.A'&gt;&gt; </code></pre> <p>Here, with a staticmethod, even though it is a method, <code>a.static_foo</code> just returns a good 'ole function with no arguments bound. <code>static_foo</code> expects 1 argument, and <code>a.static_foo</code> expects 1 argument too.</p> <pre><code>print(a.static_foo) # &lt;function static_foo at 0xb7d479cc&gt; </code></pre> <p>And of course the same thing happens when you call <code>static_foo</code> with the class <code>A</code> instead.</p> <pre><code>print(A.static_foo) # &lt;function static_foo at 0xb7d479cc&gt; </code></pre>
 

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