I have a Python (3) structure like following:
- main_script.py
- util_script.py
- AccessClass.py
The main
script is calling a function in util
with following signature:
def migrate_entity(project, name, access=AccessClass.AccessClass()):
The call itself in the main script is:
migrate_entity(project_from_file, name_from_args, access=access_object)
All objects do have values when the call is done.
However, As soon as the main
script is executed the AccessClass
in the function parameters defaults is initialized, even though it is never used. For example this main
script __init__
will create the default class in the function signature:
if __name__ == "__main__":
argparser = argparse.ArgumentParser(description='Migrate support data')
argparser.add_argument('--name', dest='p_name', type=str, help='The entity name to migrate')
load_dotenv()
fileConfig('logging.ini')
# Just for the sake of it
quit()
# The rest of the code...
# ...and then
migrate_entity(project_from_file, name_from_args, access=access_object)
Even with the quit()
added the AccessClass
is created. And if I run the script with ./main_script.py -h
the AccessClass
in the function signature is created. And even though the only call to the function really is with an access object I can see that the call is made to the AccessClass.__init__
.
If I replace the default with None
and instead check the parameter inside the function and then create it, everything is working as expected, i.e. the AccessClass
is not created if not needed.
Can someone please enlighten me why this is happening and how defaults are expected to work?
Are parameter defaults always created in advance in Python?