What would be the best way to check if a variable was passed along for the script:
try:
sys.argv[1]
except NameError:
startingpoint = 'blah'
else:
startingpoint = sys.argv[1]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the end, the difference between
try, exceptand testinglen(sys.argv)isn’t all that significant. They’re both a bit hackish compared toargparse.This occurs to me, though — as a sort of low-budget argparse:
You could even use it to generate a
namedtuplewith values that default toNone— all in four lines!In case you’re not familiar with
namedtuple, it’s just a tuple that acts like an object, allowing you to access its values usingtup.attributesyntax instead oftup[0]syntax.So the first line creates a new
namedtupletype with values for each of the values inarg_names. The second line passes the values from theargsdictionary, usinggetto return a default value when the given argument name doesn’t have an associated value in the dictionary.