samedi 7 août 2021

Is this way to import useful?

Recently, I was doing some random things with the exec function, and I stumbled across this:

When you import a file and it has if __name__ == "__main__" condition, it prevents the file from running when imported.

So to bypass that, you can use this:

file = open("filename.py", "r").read()
exec(file)

This will run the file even though the file has if __name__ == "__main__" condition. This is also an alternative to import a module.

So my question was: Is this useful in any way?

Aucun commentaire:

Enregistrer un commentaire