Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 910
user73383 Avatar asked May 11 '26 11:05

user73383


1 Answers

It's possible in Python 3.0+, after implementation of PEP 3102 -- Keyword-Only Arguments. The syntax would be exactly how you've shown it, in fact.

The usual workaround for Python 2 is this:

def saveValues(file, *data, **kwargs):
    delim = kwargs.pop('delim', '|')
    ...
like image 173
wim Avatar answered May 13 '26 01:05

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!