As I don't have much experience in Python, I'm always trying to follow the Google Python Style Guide. The guide contains the following sentence.
"Even a file meant to be used as a script should be importable and a mere import should not have the side effect of executing the script's main functionality."
Therefore, I searched for a way to override __getattr__
, and have been using this ArrtDict class for arguments parsing as follows.
import argparse
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def parse_args(args):
if isinstance(args, list):
parser = argparse.ArgumentParser()
parser.add_argument('--args1')
return parser.parse_args(args)
else:
return AttrDict(args)
def main(args):
args = parse_args(args)
if __name__ == '__main__':
import sys
main(sys.argv[1:])
What would be the best practice for arguments parsing in Python?