1
1
1Bragim0ff2020-06-18 17:09:36
Python
1Bragim0ff, 2020-06-18 17:09:36

Python default options?

I ran into an interesting problem.

def change(proxy=get()['proxy'], gmail=get()['gmail'], password=get()['password'], json_active=os.path.isfile(JSON_PATH)):
    ''' "change" changes the values ​​of config.json '''
    print(json_active)
    print(os.path.isfile(JSON_PATH))
    
    config = {
        'proxy': proxy,
        'gmail': gmail,
        'password': password,
        'json_active': json_active
    }

    with open(f'{os.getcwd()}\\config\\config.json', 'w') as file:
        json.dump(config, file, indent=1)

I have a change function, the result of the isfile method from the os module is passed to the json_active parameter.
This method checks if the file exists and returns True or False depending on this.

Now, I'm importing my change function into my main.py file.

def main():
    clear_terminal.clear()

    config.change()
    data = config.get()

    flag = input('>>> ')

    if flag == '1':
        
        USER_EMAIL = input('Your Gmail: ')

        number_combinations = email_combinate.combinate(USER_EMAIL)
        print(f'JSON generated, size: {number_combinations}')
        input('Press ENTER to continue')

        main()

When you run the main() function from the main.py file somewhere at the start, the change function is called. And config.json changes.
So, if a file is created while main.py is running, then the json_active parameter in the change function is still False.

But, if the change function is run without importing, then everything works out with a bang.

There are suspicions that Python remembers the default parameter values ​​and assigns the last parameter value to them the next time, and all this because of the compiled .pyc files. Could this be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-06-18
@1Bragim0ff

Default parameter values ​​are defined only once at the time of script execution or module import.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question