What would be the best way to check if a variable was passed along for the script:
try: sys.argv except NameError: startingpoint = 'blah' else: startingpoint = sys.argv
In the end, the difference between
try, except and testing
len(sys.argv) isn't all that significant. They're both a bit hackish compared to
This occurs to me, though -- as a sort of low-budget argparse:
arg_names = ['command', 'x', 'y', 'operation', 'option'] args = dict(zip(arg_names, sys.argv))
You could even use it to generate a
namedtuple with values that default to
None -- all in four lines!
Arg_list = collections.namedtuple('Arg_list', arg_names) args = Arg_list(*(args.get(arg, None) for arg in arg_names))
In case you're not familiar with
namedtuple, it's just a tuple that acts like an object, allowing you to access its values using
tup.attribute syntax instead of
So the first line creates a new
namedtuple type with values for each of the values in
arg_names. The second line passes the values from the
args dictionary, using
get to return a default value when the given argument name doesn't have an associated value in the dictionary.
Check the length of
if len(sys.argv) > 1: blah = sys.argv else: blah = 'blah'
Some people prefer the exception-based approach you've suggested (eg,
try: blah = sys.argv; except IndexError: blah = 'blah'), but I don't like it as much because it doesn't “scale” nearly as nicely (eg, when you want to accept two or three arguments) and it can potentially hide errors (eg, if you used
blah = foo(sys.argv), but
foo(...) raised an
IndexError would be ignored).