Does import call __new__ static method?
<p>I know that <code>__new__</code> method is called when attempt to create an instance of a class before <code>__init__</code> is called.</p>

<p>But i happened to find that, import a module withou create instance will also call <code>__new__</code></p>

<p>suppose i have this:</p>

<p>a.py:</p>

<pre><code>import abc
class A(abc.ABCMeta):
        def __new__(cls, name, bases, namespace):
            print("ttt:", cls, name, bases, namespace)
            retval = abc.ABCMeta.__new__(cls, name, bases, namespace)
            return retval


    class B(object):
        __metaclass__ = A
</code></pre>

<p>and</p>

<p>b.py</p>

<pre><code>import a

class C(B):
   def a():
      pass
</code></pre>

<p>Then i execute <code>python b.py</code>, i can see two <code>ttt</code> print by <code>__new__</code>.
So, when does the <strong>new</strong> exactly called, in this case, i never create any instance of the three classes i defined</p>

<p>When you use a metaclass to define a class, the metaclass is "called" implicitly (which invokes <code>__new__</code> since the metaclass here is an actual class), see <a href="https://www.python.org/dev/peps/pep-3115/" rel="nofollow">Invoking the metaclass</a>. I can't say why you see three prints here (you only have two classes that uses <code>A</code> as a metaclass, directly or indirectly through inheritance), but that explains two of the <code>print</code>s.</p>

python class metaclass