mercredi 3 août 2016

Properly managing many variables and their scope in Python

My Python script needs about 20 paths / filenames that share common base paths. Until now, these have been set globally, but now they need to be modified on user input:

globalbase = "thispath/"
globalvar_a = globalbase + "file.ext"
...
globalvar_z

myClass
    myMethod()
        write(globalvar_a)

cli()
    globalbase = "otherpath/"

main()
    cli()

So the paths need to be created in / after cli() which forces them out of the module scope and thus they are not available in myClass / myMethod. Making all of them global would become ugly.

What are common programming / design patterns to handle this situation?

Aucun commentaire:

Enregistrer un commentaire