0

I am trying to pass both positional and optional parameters to a function.

The below code works in python 3:

def something(*args, cookie=None):            
    for arg in args:
        print(arg)
    print('cookie is ' + cookie)

something('a', 'b', 'c', cookie='oreo')

but it gives

SyntaxError: invalid syntax

when using python 2.

Is something like this possible in python 2?

ritiek
  • 2,477
  • 2
  • 18
  • 25

1 Answers1

1

If you modify the function slightly, you can make it work. Looks like python2 doesn't support the exact syntax that you were hoping for. The syntax that you used works in python3 though.

def something(*args, **kwargs):
    for arg in args:
        print(arg)
    print('cookie is ' + kwargs['cookie'])
Jonathan
  • 8,453
  • 9
  • 51
  • 74