Say I have a namespace args
that I obtain from calling parser.parse_args()
, which parses the command line arguments.
How can I import all variables from this namespace to my current namespace?
e.g.
parser.add_argument('-p', '--some_parameter', default=1)
args = parser.parse_args()
# ... code to load all variables defined in the namespace args ...
print some_parameter
I could certainly do:
some_parameter = args.some_parameter
but if I have a large number of parameters I would need one such line for each parameter.
Is there another way of importing variables from a namespace without having to go through them one by one?
PS: from args import *
does not work.
PS2: I am aware that this is a bad practice, but this can help in some corner cases, such us when prototyping code and tests very quickly.
__dict__
or useinspect
... – sr2222 8 hours agolocals().update(namespace._get_kwargs())
. – abarnert 8 hours agoargs.some_parameter
? (Especially since, in a non-trivial program, you're probably going to want to pass the options to other functions, which means if you've got lots of options you're probably going to end up building adict
or other object equivalent to the namespace you pulled apart…) – abarnert 8 hours agovars(args)
to get adict
– Francesco Montesano 8 hours ago