This is really a few questions:
Is there a reason argparse uses a namespace instead of a dictionary?
Assuming I have a class with __init__(self, init_method, *args)
. The init_method
parameter tells the init_function which way I want to initialize the class, while arg
parameter gives all the arguments neccesary for the init. The arguments may be different for different methods. Should I use a dictionary, or a namespace?
Assuming that I use a namespace, how do I pass the namespace to __init__()
?
Class argparse. Namespace is a simple class used for creating an object by parse_args() which holds the attributes and returns it. This class is a subclass of Object class with a very simple and readable string representation. We can get a dictionary-like structure of the attributes stored in it using vars() in Python.
You can use the argparse module to write a command-line interface that accepts a positional argument. Positional arguments (as opposed to optional arguments, which we'll explore in a subsequent section), are generally used to specify required inputs to your program.
So if you want to create a namespace, you just need to call a function, instantiate an object, import a module or import a package. For example, we can create a class called Namespace and when you create an object of that class, you're basically creating a namespace.
It is easy to convert a Namespace into a dictionary using vars():
>>> vars(args)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With