argparse.parse_args() vs argparse.Namespace
argparse.parse_args() and argparse.Namespace are both Python Argparse Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. argparse.parse_args(): Parses the command-line arguments and returns a Namespace object. Its syntax is argparse.parse_args(args=None, namespace=None), and it returns namespace object argparse.Namespace: The default namespace object used by parse_args(). Its syntax is argparse.Namespace(**kwargs), and it returns namespace object Example of argparse.parse_args(): args = parser.parse_args(["--output", "out.txt", "in.txt"])
args.output # "out.txt"
args.input # "in.txt". Example of argparse.Namespace: ns = argparse.Namespace(input="file.txt", verbose=True). Read the full references for argparse.parse_args() and argparse.Namespace to see parameters, edge cases, and example code side by side. Other Argparse Module APIs worth comparing: argparse.ArgumentParser(), argparse.add_argument(). Both belong to the Argparse Module category in the Python Reference documentation. Try both in the Python playground to see the behavioral difference live.