How Python fills a string from argparse

I am wondering what happens behind the scenes when using argparse. I checked here and here , as apparently the namespace currently exists only in the argparse library .

I may be using the wrong keywords to search for SO / Google. It is also possible that I am asking a pointless or obvious question, but here we go.

When capturing an input string in Python via argparse as such:

>python palindrome.py 'Taco cat!?' 

When I run the code below, I expect that by specifying parser.add_argument ('string' ..., the resulting namespace acts as a buffer for a single input string.

The next line, in which I assign the string "args", should be the first time we actually parse the input, resulting in a workload proportional to the length of the input string. At the moment, "args" actually contains a namespace object that cannot be parsed through a loop or otherwise (which I know).

Finally, to parse the input using an β€œfor” or some other loop, I use a namespace object to populate the string. I am curious how many times this process takes a calculation time proportional to the original string length?

Which of these steps is being copied to or copied by value behind the scenes? It seems like an optimistic worst case would be 2x. Once, to create a namespace object, then again to assign its contents to "arg_str"

 #! /usr/bin/env python import sys import argparse parser = argparse.ArgumentParser(description='Enter string to see if it\ a palindrome.') parser.add_argument('string', help="string to be tested for palindromedness..ishness") args = parser.parse_args() arg_str = args.string # I can parse by using 'for i in arg_str:' but if I try to parse 'for i in args:' # I get TypeError: "Namespace' object is not iterable 

Thanks for watching !!

+7
python string parsing user-input argparse
source share
2 answers

The operating system (or shell) first parses the command line, passing the lines to the Python interpreter, where they are available to you as an sys.argv array.

 python palindrome.py 'Taco cat!?' 

becomes

 ['palindrome.py', 'Taco cat!?'] 

parser.parse_args() processes these lines, usually just passing the links around. When the base argument is β€œparsed”, this string is β€œstored” in the namespace with setattr(Namespace, dest, value) , which in your example would be equivalent to setattr(namespace, 'string', sys.argv[1]) .

There is nothing special about argparse.Namespace . This is a simple subclass of Object . Arguments are simple attributes of an object. argparse uses setattr and getattr to access them, although users can usually use the dot format ( args.string ). It does not perform special string processing. This is entirely Python's responsibility.

The namespace is not iterable, that is, it is not a list or tuple, or something like that. This is an object. The namespace can be converted to a dictionary with vars(args) (this is in the argparse documentation). This way you can iterate over this dictionary with keys and items .

One more thing. Do not test arg.string with is . Use == or in [] to compare it with other lines. This is because the line created with sys.argv does not have the same id as the one created with x = 'test' . To understand why, try:

 argv = 'one two three'.split() print argv[0]=='one' # true print argv[0] is 'one' # false print argv[0] in ['one', 'two','three'] # true x = 'one' print x is 'one' # true print id(x) print id('one') print id(argv[0]) 

Whenever possible, Python stores unique copies of strings. but if the strings are generated differently, they will have different identifiers and do not satisfy the is test.

+1
source share

A Python assignment never makes copies of things. Variables are the names of objects; assigning an object to a variable gives the object a new name (taking the name from what it was before). In low-level expressions, this is a copy of the pointer and several recalculation operations.

No part of this code requires copying from the input string. If that were so, it would not matter. The command line arguments cannot (and should not) be long enough for the time when you could spend copying to be significant compared to the rest of your runtime.

+1
source share

All Articles