How to iterate over arguments

Please add 'vars' if you wanna iterate over namespace object:

 for arg in vars(args):
     print arg, getattr(args, arg)

Namespace objects aren't iterable, the standard docs suggest doing the following if you want a dictionary:

>>> vars(args)
{'foo': 'BAR'}

So

for key,value in vars(args).iteritems():
    # do stuff

To be honest I'm not sure why you want to iterate over the arguments. That somewhat defeats the purpose of having an argument parser.


After

args = parser.parse_args()

to display the arguments, use:

print args # or print(args) in python3

The args object (of type argparse.Namespace) isn't iterable (i.e. not a list), but it has a .__str__ method, which displays the values nicely.

args.out and args.type give the values of the 2 arguments you defined. This works most of the time. getattr(args, key) the most general way of accessing the values, but usually isn't needed.

vars(args)

turns the namespace into a dictionary, which you can access with all the dictionary methods. This is spelled out in the docs.

ref: the Namespace paragraph of the docs - https://docs.python.org/2/library/argparse.html#the-namespace-object