Why is `self` not used in this method?
<p>I was under the impression that methods within Python classes <em>always</em> require the <code>self</code> argument (I know that it doesn't actually have to be <code>self</code>, just some keyword). But, this class that I wrote doesn't require it:</p>

<pre><code>import ZipFile
import os
class Zipper:
    def make_archive(dir_to_zip):
        zf = zipfile.ZipFile(dir_to_zip + '.zip', 'w')
        for filename in files:
            zf.write(os.path.join(dirname, filename))
        zf.close()
</code></pre>

<p>See? No <code>self</code>. When I include a <code>self</code> argument to <code>make_archive</code>, I get a <code>TypeError: make_archive() missing one positional argument</code> error. In my search to figure out why this is happening, I actually copied and tried to run a similar program from the docs:</p>

<pre><code>class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'

print(MyClass.f())  # I added this statement to have a call line
</code></pre>

<p>and I get the same error! </p>

<pre><code>TypeError: f() missing 1 required positional argument: 'self'
</code></pre>

<p>In the same module that contains the <code>Zipper()</code> class, I have multiple classes that all make use of <code>self</code>. I don't understand the theory here, which makes it difficult to know when to do what, especially since a program copied directly from the docs (<a href="https://docs.python.org/3/tutorial/classes.html" rel="nofollow">this is the docs page</a>) failed when I ran it. I'm using Python 3.5 and 3.4 on Debian Linux. The only thing that I can think of is that it's a static method (and the <code>Zipper.make_archive()</code> as written above works fine if you include <code>@staticmethod</code> above the <code>make_archive</code> method), but I can't find a good explanation to be sure.</p>

<p>The thing with <code>self</code> is that it's added <em>implicitly</em>. That is, the calling code says <code>Myclass().f()</code>, but the callee sees <code>Myclass().f(self)</code>. It also implies that the method is called from some instance of <code>Myclass</code>, which is placed in <code>self</code> variable. The point is that methods are probably somehow using and/or modifying instance data (otherwise why would they be in that class?) and it's handy to have the instance in question automatically supplied.</p>

<p>If you don't need the instance data, you should use either <code>@staticmethod</code> if it's actually more like a function than object method or <code>@classmethod</code> if the method is meant to be inherited and possibly used differently by different classes. See @pankaj-daga answer for a little intro to staticmethods.</p>

<p>The <code>Foo.bar()</code> syntax is also used by functions imported via <code>import Foo</code> instead of <code>from Foo import bar</code>, which is also a possible source of confusion. That, for your purposes, is an entirely different thing.</p>

python python-3.x typeerror self