title: Why import class from another file will call __init__ function?
body: <p>The structure of the project is:</p>
  
  <p>project<br>
  - main.py<br>
  - session.py<br>
  - spider.py</p>
  
  <p>There is a class in session.py:</p>
  
  <pre><code>import requests
  
  class Session:
  
      def __init__(self):
          self.session = requests.Session()
          print('Session created.')
  </code></pre>
  
  <p>And another class in spider.py:</p>
  
  <pre><code>from session import Session
  
  class Spider:
  
      def __init__(self, sess: Session = Session()):
          print('Spider created.')
  </code></pre>
  
  <p>When I import class <code>Spider</code> from spider.py in main.py like this:</p>
  
  <pre><code>from spider import Spider
  
  if __name__ == '__main__':
  
      print('Main function.')
      spider = Spider()
  </code></pre>
  
  <p>And run main.py, I get:</p>
  
  <blockquote>
    <p>Session created.<br>
    Main function.<br>
    Spider created.  </p>
  </blockquote>
  
  <p>It confuses me. I think <code>__init__</code> is the initial function used when initializing an instance, but in this case the <code>__init__</code> function of <code>Session</code> is called when <code>Session</code> is imported in spider.py. I think it must be related to the default value of <code>__init__</code> function in spider.py, but why?</p>