Is there any reason for giving self a default value?
<p>I was browsing through some code, and I noticed a line that caught my attention. The code is similar to the example below</p>

<pre><code>class MyClass:
    def __init__(self):
        pass

    def call_me(self=''):
        print(self)
</code></pre>

<p>This looks like any other class that I have seen, however a <code>str</code> is being passed in as default value for <code>self</code>. </p>

<p>If I print out <code>self</code>, it behaves as normal</p>

<pre><code>&gt;&gt;&gt; MyClass().call_me()
&lt;__main__.MyClass object at 0x000002A12E7CA908&gt;
</code></pre>

<p>This has been bugging me and I cannot figure out why this would be used. Is there any reason to why a <code>str</code> instance would be passed in as a default value for <code>self?</code></p>

<p>The short answer is yes. That way, you can call the function as: </p>

<pre><code>MyClass.call_me()
</code></pre>

<p>without instantiating <code>MyClass</code>, that will print an empty string.</p>

<p>To give a longer answer, we need to look at what is going on behind the scenes.</p>

<p>When you create an instance of the class, <code>__new__</code> and <code>__init__</code> are called to create it, that is:</p>

<pre><code>a = MyClass()
</code></pre>

<p>is roughly equivalent to:</p>

<pre><code>a = MyClass.__new__(MyClass)
MyClass.__init__(a)
</code></pre>

<p>Whenever you use some method on a created instance <code>a</code>:</p>

<pre><code>a.call_me()
</code></pre>

<p>It is "replaced" with <code>MyClass.call_me(a)</code>.</p>

<p>So, having a default parameter for <code>call_me</code> allows you to call this function not only as a method of an instance, in which case <code>self</code> is an instance itself, but also as a static class method.</p>

<p>That way, instead of <code>MyClass.call_me(a)</code>, just <code>MyClass.call_me()</code> is called. Because the argument list is empty, the default argument is assigned to <code>self</code> and the desired result (empty string) is printed.</p>

python class python-3.x