Reputation: 3387
I notice that Python frequently uses strings where you could use some kind of defined symbol, like a constant. For example, to paraphrase a sample of code from argparse
:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
Rather than requiring the use of the string 'store_const'
, why didn't the developers define a constant-like symbol, e.g. ArgumentParser.STORE_CONST
?
I'd prefer the constant because code editors can easily see them in the module and use them as suggestions for code completions. Also, a typo in a string may not cause an error, but a typo in a defined symbol probably would. I've seen several examples in Python of strings used this way, but it's not as common in other languages, like Java or PHP.
Upvotes: 0
Views: 56
Reputation: 113930
many libraries do use "constants" ... including argparse
you could just as easily pass in argparse._StoreConstAction
it also provides the following named constants
SUPPRESS = '==SUPPRESS=='
OPTIONAL = '?'
ZERO_OR_MORE = '*'
ONE_OR_MORE = '+'
PARSER = 'A...'
REMAINDER = '...'
_UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args'
another option would be to write your own constants class that encompasses all of the magic numbers and or strings that bother you
Upvotes: 1