3

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.

3
  • 1
    Have you tried this out as-is? I think it should work. Commented Aug 5, 2016 at 18:57
  • @MattCremeens that's a syntax error Commented Aug 5, 2016 at 18:59
  • Nope... doesn't work in 2.7... may work in 3, but I'm on an imbedded device where I got to use what I have. Commented Aug 5, 2016 at 19:31

1 Answer 1

7

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', '|')
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.