user73383 user73383 - 3 months ago 9
Python Question

Python Using Keyword and variable number of arguments in same function

I am wondering if there is a way to do something like this in python 2.7.12

def saveValues(file,*data,delim="|"):
buf=""
for d in data:
buf+=str(d) + delim
open(file,"w").write(buf[:-1])


So that I have the option to pass delim, or take the default.

wim wim
Answer

It's possible in python3. The python2 workaround is usually this:

def saveValues(file, *data, **kwargs):
    delim = kwargs.pop('delim', '|')
    ...
Comments