How to set default value for stellar arguments?

I have a function that accepts *args, but I would like to set the default tuple if none are provided. (This is not possible with the help def f(*args=(1, 3, 5))that a calls SyntaxError.) What would be the best way to do this? The proposed functionality is shown below.

f()
# I received 1, 2, 3!

f(1)
# I received 1!

f(9, 3, 72)
# I received 9, 3, 72!

The following function gwill provide the correct functionality, but I would prefer *args.

def g(args=(1, 2, 3)):
    return "I received {}!".format(', '.join(str(arg) for arg in args))

g()
# I received 1, 2, 3!

g((1,))
# I received 1!

g((9, 3, 72))
# I received 9, 3, 72!
+4
source share
3 answers

, args . , , args .

def g(*args):
    if not args: 
        args = (1, 2, 3)
    return 'I received {}!'.format(', '.join(str(arg) for arg in args))
+6

, args :

def g(*args):
    if not args:
        args = (1, 2, 3)
    return "I received {}!".format(', '.join(str(arg) for arg in args))

no args , , False.

+11
def g(*args):
    if not args:
        args = [1, 3, 5]
    return "I received {}!".format(', '.join(str(arg) for arg in args))
+4
source

All Articles