lundi 26 juillet 2021

How to skip loading file if it is alrerady loaded in memory using Python?

I have the following code where I am trying to avoid loading symspell.pkl file once it is already loaded in memory:

from symspellpy import SymSpell

if 'sym_spell' in globals():
    print('sym_spell is already loaded!')
    sym_spell = global()['sym_spell']
esle:
    print('loading sym_spell...')
    sym_spell = SymSpell(max_dictionary_edit_distance=5, prefix_length=7)
    sym_spell.load_pickle('symspell.pkl')

but it seems python always executing the else statement even if the if statement is True.

Following are my questions:

  • Can anyone explains why Python is always executing else statement here?

  • Is what I tried a right way to skip loading file if it is already loaded in memory in Python? If not, is there a better way?

I am using python3.8.2.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire