1

I would like to create a function which can take either 1 or 2 arguments. Currently, I have a function which takes exactly 2 arguments through CMD:

def test(self,countName,optionalArg):
        if countName == "lowest":
           #something
        if optionalArg == "furthest:
           #something
        else:
           #something else

if __name__ == '__main__':
        countName = sys.argv[1]
        optionalArg = sys.argv[2]

        temp = len(sys.argv)
        for i in xrange(1,temp):

            sys.argv.pop()

I would then run:

python filename.py lowest furthest

Using this means that passing the second arg is a must. If I try to run my script just by passing one arg, it encounters an error (as expected). My question is, how do you create an optional argument, which could either be passed or not, depending on the situation?

For example:

python filename.py lowest

In this situation, I expect the program to perform the "#something else" script, as nothing was passed and it is different than "furthest".

Please do not write the code for me, I am here to learn :)

Kfir Cohen
  • 153
  • 1
  • 3
  • 13
  • Consider having your function accept one argument, that is in itself a list of arguments. That way you can do different things, depending on the length of that list. – Christofer Ohlsson Feb 08 '16 at 08:35
  • @ChristoferOhlsson Hi Christoper, if I change the function to accept only one argument, then it disregards the things I am writing through CMD, thus not letting me pass a second argument. – Kfir Cohen Feb 08 '16 at 08:38
  • If you keep passing it two arguments, then sure. But why would you? – Christofer Ohlsson Feb 08 '16 at 08:41
  • @ChristoferOhlsson Because I would like to perform different calculations. If "furthest" was passed, then do something, but if nothing was passed, do something else. I could pass "zzz" through CMD to not receive the error and make it work, however, this seems like a very ugly solution. – Kfir Cohen Feb 08 '16 at 08:48
  • If you plan on using different options on command-line, some of which are optionals, with possibly different types of input, you **should** read about the `argparse` module. – DainDwarf Feb 08 '16 at 08:49
  • Which works just fine with my suggestion. You do pretty much what I'm suggesting with the argv list. – Christofer Ohlsson Feb 08 '16 at 08:49
  • Possible duplicate of [Is there a way to pass optional parameters to a function?](https://stackoverflow.com/questions/14017996/is-there-a-way-to-pass-optional-parameters-to-a-function) – PenguinEngineer Nov 02 '17 at 17:34

7 Answers7

4

This is explained in the FineManual(tm): https://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions

Note that in Python, the expression defining the default value for an optional argument is eval'd ony once when the def statement is executed (which is at first import for a top-level function), which can lead to unexpected behaviours (cf "Least Astonishment" and the Mutable Default Argument).

Also, the "default value" has to be an expression, not a statement, so you cannot do any error handling here. wrt/ your case with trying to use sys.argv[2] as a default value, it's wrong for at least two reasons:

  1. as you already noticed, it breaks if len(sys.argv) < 3
  2. it makes your function dependent on sys.argv, so you cannot reuse it in a different context

The right solution here is to handle all user input (sys.argv or whatever) in the "entry point" code (the __main__ section) - your function should know nothing about where the arguments values came from (sys.argv, an HTTP request, a text file or whatever).

So to make a long story short: use either a hardcoded value (if it makes sense) or a "sentinel" value (None is a good candidate) as default value for your optional argument, and do all the user inputs parsing in the __main__ section (or even better in a main() function called from the __main__ section so you don't pollute the module's namespace with irrelevant variables):

def func(arg, optarg=None):
    #code here


def main(*args):
    #parse args
    #call func with the right args

if __name__ == "__main__":
    import sys 
    main(*sys.argv)
Community
  • 1
  • 1
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • Hi, thank you for your help. I have read it and couldn't quite figure out how to resolve my issue using this. I have updated my question, would appreciate if you could review it. I believe my issue relies in "optionalArg = sys.argv[2]", as it is always expecting a statement there. not passing anything through the CMD gives a "list index out of range" error. – Kfir Cohen Feb 08 '16 at 08:58
  • I'm terribly sorry it took me so much time to respond, personal issues. Thank you very much, this greatly assisted me! I managed to convert my code and use the (*args) in the way mentioned. One thing is still unclear to me, and that is how do I check the args that were passed. I can't use 'if args[1]' since it also breaks the code, if no args were passed. Then how am I supposed to check what arg was actually passed? – Kfir Cohen Mar 01 '16 at 08:59
  • When using `def foo(*args)`, in `foo`, `args` will be a `tuple`. The very first thing to do is of course to check it's length and take appropriate action if no args were actually passed. Also note that there are a couple command line arguments parsers in the standard lib... – bruno desthuilliers Mar 01 '16 at 14:44
  • Perfect. Thank you very much! – Kfir Cohen Mar 13 '16 at 08:28
2

You can write your function by providing default argument value to the argument you want to ignore like optionalArg=None(whatever you want) by doing this you can call the function with single argument.

Dharmik
  • 445
  • 3
  • 10
0

A very, VERY ugly way is to use exceptions for check if the parameter is defined:

import sys

if __name__ == '__main__':
    arg = sys.argv[1]

    try:
        optionalArg = sys.argv[2]
    except IndexError:
        optionalArg = ""
    else:
        print "sure, it was defined."

I advise you not to use it, because you should never be in a situation where you don't know if a variable is defined or not, there are better ways to handle this, but in some cases (not yours) can be usefull, I add it only for this reason.

k4ppa
  • 4,122
  • 8
  • 26
  • 36
0

Something like this? kinda code sorry :D

def function(value, opt = ""):
  print("Value: " + value)
  if not opt == "":
    print("Optional: " + opt)
Device
  • 574
  • 4
  • 19
0

You can pass a dictionary. Or a default value which is None if you not explicitly initialize it when calling the function.

Supahupe
  • 515
  • 2
  • 11
0

Variant:

def somefunc(*args, **kwargs):
   if 'optional_arg' in kwargs:
       print kwargs['optional_arg']
Maxim Panfilov
  • 371
  • 4
  • 15
0

You can use *args to pass any number of arguments and receive them as a list, or use **kwargs to send keyword arguments and receive them as a dictionary.

Here is an example for *args:

def test(count_name, *args):

    if count_name == "lowest":    
        print('count_name received.')  

    if args and args[-1] == "furthest":    
        print('2nd arg received.')    

    else:    
        print('No second arg given.')    

    return None


if __name__ == '__main__':
    count_name = 'lowest'
    optional_arg = 'furthest'

    print('Test 1:')
    test(count_name, optional_arg)
    # Displays:
    # Test 1:
    # count_name received.
    # 2nd arg received.

    print('\n\nTest 2:')
    test(count_name)
    # Test 2:
    # count_name received.
    # No second arg given.

This is how you pass optional arguments, not by defining a default value for a variable, which initialises the variable in the memory.

Pouria
  • 1,081
  • 9
  • 24