The structure of the project is:
project
- main.py
- session.py
- spider.py
There is a class in session.py:
import requests
class Session:
def __init__(self):
self.session = requests.Session()
print('Session created.')
And another class in spider.py:
from session import Session
class 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 Spider
if __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?
The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
Python modules can get access to code from another module by importing the file/function using import. The import statement is that the commonest way of invoking the import machinery, but it's not the sole way. The import statement consists of the import keyword alongside the name of the module.
The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.
If a file named __init__.py is present in a package directory, it is invoked when the package or a module in the package is imported. You can use this to execute package initialization code, for example for the initialization of package-level data.
The default values of parameters get evaluated only once in python. This is documented here and also here as stated by JETM.
Therefore a isntance of Session
is created when you import spider
as default value for the sess
parameter of the spiders __init__
method.
If you do not wish such a behavior you can use None
as default value and create the Sesssion
instance inside the __init__
method if no other value was provided as pointed out by MatsLindh and FHTMitchel like this:
...
def __init__(self, sess: Session = None):
if sess is None:
sess = Session()
print('Spider created.')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With