Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why import class from another file will call __init__ function?

Tags:

python

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?

like image 612
Thesharing Avatar asked Oct 10 '18 11:10

Thesharing


People also ask

What is the purpose of the __ init __ function in a class?

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.

How do you call a class from another file in Python?

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.

Why do I need __ init __ PY?

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.

Can I import from __ init __ PY?

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.


1 Answers

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.')
like image 91
kalehmann Avatar answered Oct 02 '22 12:10

kalehmann