On this page
argparse.Namespace
Prev: argparse.MetavarTypeHelpFormatter | Up: argparse | Next: argparse.RawDescriptionHelpFormatter
Rel: argparse.Action, argparse.ArgumentDefaultsHelpFormatter, argparse.ArgumentError, argparse.ArgumentParser
Ref: docs.python.org — argparse.Namespace
Tags: #public #python3 #stdlib #argparse
Import
import argparse
from argparse import Namespace
Signature & Overview
argparse.Namespace(**kwargs)
Simple object for storing attributes.
Usage
import argparse
ns = argparse.Namespace(host="localhost", port=8080)
assert ns.host == "localhost" and vars(ns) == {"host": "localhost", "port": 8080}
Gotchas
- Convert to dict with
vars(args): Useful for logging parsed CLI options or passing them to a config builder. - Equality compares attribute dicts: Great for assertions in unit tests (
assert parser.parse_args(...) == Namespace(...)).
REPL Inspect
from argparse import Namespace
help(Namespace)
Linked from
Related notes
- tuts/python3/stdlib_module/argparse/MetavarTypeHelpFormatter Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/RawDescriptionHelpFormatter Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse/Action Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentDefaultsHelpFormatter Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentError Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentParser Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentTypeError shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/BooleanOptionalAction shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/FileType shared tags: #argparse, #stdlib