I have a Python file with several function definitions in it. One of the functions, named 'main' will be called as soon as the Python file is run.


import sys
def main(arg1....):

--more functions--


When a person wants to run my file they'll type:

python myFile.py arg1 arg2 ...

The main function is supposed to accept in x number of arguments, however, in case the user doesn't wish to pass in any arguments we're supposed to have default values.

My program looks something like this and I'm hoping there is actually a better way to do this than what I have:

import sys
#Even though my function has default values, if the user doesn't wish
#to input in any parameter values, they still must pass in the word False
#otherwise, pls pass in parameter value
def main(name = "Bill", age = 22, num_pets=5, hobby = "soccer"):
   if len(sys) > 1:
      i =0
      while i < len(sys):
         if i == 0:
            if sys.argv[i] == "False":
               i += 1
                name = sys.argv[i]
                i += 1
         elif i == 1:
            if sys.argv[i] == "False":
     --------etc. etc.-----------

closed as off-topic by 200_success Dec 1 '15 at 20:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.


In case the number of arguments is really long then you can use **kwargs to avoid cluttering up arguments in function definition block. I would also avoid naming the function as "main" and replace it with something like "execute".

def execute(**kwargs):
  name = kwargs.get('name', 'Bill')
  # get other values

if __name__ = "__main__":

You can use the apply operator (*):

import sys

def main(name = "Bill", age = 22, num_pets=5, hobby = "soccer"):

if __name__ = "__main__":

You can also use argparse to sanitize the contents of sys.argv before applying them to main().


You can also use optparse

Here is an example:

import optparse
if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option('-s', '--string', dest = 'someVar', default = 'default', help = '-s with a small s. With default Value')
    parser.add_option('-S', '--boolean', action = 'store_true', dest = 'someBool', help = 'i am a flag :). Use me with a capital S')

    (options, args) = parser.parse_args()


Then your users have to call it like

python script.py -s HerpDerp


python script.py -h

to see a list of available arguments

  • \$\begingroup\$ optparse seems to be deprecated since version 2.7 \$\endgroup\$ – Kinjal Dec 18 '12 at 10:19
  • \$\begingroup\$ @Kinjal You are right. But there is argparse which has a similar syntax. \$\endgroup\$ – user1882585 Dec 18 '12 at 10:42

I'd suggest using high-level fool-proof libraries like argparse.

The problem with argparse is that its syntax is too verbose, so I developed Argh. It helps maintain the code simple and pythonic:

import argh

def main(name, age=22, num_pets=5, hobby='soccer'):
    yield 'This is {name}, {age}'.format(name=name, age=age)
    yield 'He has ' + ('one pet' if num_pets < 2 else 'many pets')


Example usage:

$ ./script.py Bill
This is Bill, 22
He has many pets

$ ./script.py John -a 50 --num-pets=1 --hobby sleeping
This is John, 50
He has one pet

This can be easily tuned if you don't mind diving into the documentation.

Of course it's not the only library in this field.


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