1 Replies - 10313 Views - Last Post: 19 December 2012 - 02:49 PM Rate Topic: -----

#1 evthisal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 12

default argument and parameter values in python

Posted 17 December 2012 - 03:30 PM

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 ...

My question:
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.-----------

Is there a better way to do this?

Thank you in advance

This post has been edited by atraub: 19 December 2012 - 02:26 PM

Is This A Good Question/Topic? 0
  • +

Replies To: default argument and parameter values in python

#2 modi123_1   User is offline

  • Suitor #2
  • member icon

Reputation: 14092
  • View blog
  • Posts: 56,460
  • Joined: 12-June 08

Re: default argument and parameter values in python

Posted 19 December 2012 - 02:49 PM

I didn't lock this, but it looks to be a dup of this one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1