swansonk14 / typed-argument-parser

Typed argument parser for Python
MIT License
497 stars 40 forks source link

[Question] Why behavior is different from original ArgumentParser's __dict__? #88

Closed jin0g closed 1 year ago

jin0g commented 2 years ago

TAP behaves differently from the original ArgumentParser's __dict__. In ArgumentParser, we can do args.__dict__ to get a dict in which only the arguments we added are enumerated. In TAP, however, the __dict__ contains a variety of additional information. The method args.as_dict() is given only to TAP, and which behaves the same as original args.__dict__ I expected. Why this difference? And is this a specification? thank you.

swansonk14 commented 1 year ago

Hi @jin0g,

Thank you for the question! Python's __dict__ exposes the variables and methods of an object. In ArgumentParser, the object only contains the variables with the parsed arguments, so __dict__ only contains those variables. However, Tap contains additional methods and variables that help perform argument parsing, so these methods and variables are also exposed as part of __dict__. This is why we built Tap's as_dict method, which returns a dictionary that only contains variables with the parsed arguments, just like ArgumentParser.

Best, Kyle and Jesse