The structure of the project is:
project
- main.py
- session.py
- spider.py
There is a class in session.py:
import requestsclass Session:def __init__(self):self.session = requests.Session()print('Session created.')
And another class in spider.py:
from session import Sessionclass Spider:def __init__(self, sess: Session = Session()):print('Spider created.')
When I import class Spider
from spider.py in main.py like this:
from spider import Spiderif __name__ == '__main__':print('Main function.')spider = Spider()
And run main.py, I get:
Session created.
Main function.
Spider created.
It confuses me. I think __init__
is the initial function used when initializing an instance, but in this case the __init__
function of Session
is called when Session
is imported in spider.py. I think it must be related to the default value of __init__
function in spider.py, but why?