Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace, argparse, and usage

Tags:

This is really a few questions:

  1. Is there a reason argparse uses a namespace instead of a dictionary?

  2. 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?

  3. Assuming that I use a namespace, how do I pass the namespace to __init__()?

like image 550
user e to the power of 2pi Avatar asked Nov 01 '11 15:11

user e to the power of 2pi


People also ask

What is args namespace in Python?

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.

What is Argparse used for?

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.

How do I create a namespace object?

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.


1 Answers

It is easy to convert a Namespace into a dictionary using vars():

>>> vars(args) 
like image 187
Raymond Hettinger Avatar answered Oct 12 '22 09:10

Raymond Hettinger