10

I have always read codes like this,

parser.add_argument('--name', action='store_true', default=False, help='XXX')

For example, this code man-sf-emnlp/train.py - midas-research

But what is the point of setting default=False when you already set action='store_true'?

7
  • 1
    It's a stylistic choice to be explicit rather than implicit. That's generally in line with the Zen of Python. Commented Dec 20, 2021 at 21:14
  • Note that Stack Overflow questions should be about a specific problem you actually face. Do you have any problem this causes? Commented Dec 20, 2021 at 21:15
  • 3
    @CharlesDuffy this is a specific question - and it doesn't have to be a question you actually face, it is perfectly fine to ask about things you don't grok, the question just have to be answerable.
    – thebjorn
    Commented Dec 20, 2021 at 21:17
  • 1
    @Charles Duffy Sorry I don't have big problems now, just having some difficulties in understanding these deep learning codes as a beginner.
    – user900476
    Commented Dec 20, 2021 at 21:17
  • 4
    I've answered a lot of SO about argparse, and don't see anything wrong with this question.
    – hpaulj
    Commented Dec 20, 2021 at 23:38

2 Answers 2

7

There's no point. From the docs:

'store_true' and 'store_false' - These are special cases of 'store_const' used for storing the values True and False respectively. In addition, they create default values of False and True respectively.

(added bold)


In the comments, Charles Duffy said, "It's a stylistic choice to be explicit rather than implicit", which is a fair point, but it also means that if you're editing the code and accidentally mismatch the action and default, it'll break:

>>> parser.add_argument('--name', action='store_true', default=True)
>>> parser.parse_args(['--name'])  # Good
Namespace(name=True)
>>> parser.parse_args([])  # Bad
Namespace(name=True)

And I think the implicit default is obvious anyway.

2
  • Not just obvious, but the only sensible default. Why use --name if it doesn't do anything different from not using --name?
    – chepner
    Commented Dec 20, 2021 at 23:52
  • @chepner Someone who's not familiar with argparse might assume that the default is to have it undefined, but I think that's unlikely.
    – wjandrea
    Commented Dec 20, 2021 at 23:57
2

The __init__ for the store_true subclass is:

class _StoreTrueAction(_StoreConstAction):

    def __init__(self,
                 option_strings,
                 dest,
                 default=False,
                 required=False,
                 help=None):
        super(_StoreTrueAction, self).__init__(
            option_strings=option_strings,
            dest=dest,
            const=True,
            default=default,
            required=required,
            help=help)

Notice that it sets default=False. The user code can override that, but what's the point? This subclass is just a store_const where the default is False and the const is True.

add_argument takes a number of keyword parameters and creates an Action subclass object. Different actions make use of different combinations of parameters. add_argument takes a casual approach to required or superfluous parameters. That is, there isn't a lot of code that checks that just the right parameters have been defined.

I'd leave it off since the default is the correct one.

Not the answer you're looking for? Browse other questions tagged or ask your own question.