Module attributes, such as sys.argv , are objects. No matter which module you are accessing from, they are the same object.
It also means that if one module modifies sys.argv , then this change will also affect any other module that accesses sys.argv .
Coding Style Tip:
Although you can access sys.argv from two different modules, I would not recommend it, and here's why.
I like scripts that can also be double as modules. This gives you maximum flexibility when reusing code. sys.argv only makes sense when the code is called as a script. In order for the code to be useful as a module, the code should not depend on finding values ββin sys.argv .
Therefore, I would recommend parsing sys.argv once in the main script call:
if __name__ == '__main__': import argparse def parse_args(): ...
and then passing the values ββto args in the function as needed.
Thus, everything that is outside the if __name__ == 'main__' should not depend on sys.argv and therefore can be used through simple function calls or importing modules.
source share