3

Let's say I have this function code:

def dosomething(thing1, thing2='hello', thing3='world'): print(thing1) print(thing2) print(thing3) 

When calling it, I would like to be able to specify what thing3 is, but without having to say what thing2 is. (The code below is how I thought it might work...)

dosomething("This says 'hello fail!'", , 'fail!') 

and it would say:

This says 'hello fail!' hello fail! 

So is there a way to do it like that, or would I have to specify thing2 every time I wanted to say what thing3 was?

3
  • So you have a function where arguments thing2,3 are named (kwarg) arguments. And the way you attempted to do this in your code was by trying to pass them by positional association, but omitting thing2; however "This...", , "fail!" is a syntax error, you can't have two commas after each other and no value. So you need named association (...thing3=='fail!'). Commented Feb 2, 2024 at 19:06
  • Yes, that's the accepted answer (from 12 years ago)! Commented Feb 3, 2024 at 22:20
  • @hiftanotiks: sure, I'm trying to help OP and other readers phrase their question in more Pythonic terms so it gets indexed better. Also, the OP never states what the actual symptom is, but having two back-to-back commas is a syntax error. Commented Feb 3, 2024 at 22:59

2 Answers 2

7

Use keyword arguments

dosomething("This says 'hello fail!'", thing3='fail!') 
Sign up to request clarification or add additional context in comments.

Comments

3

Yes, you can:

dosomething("This says 'hello fail!'", thing3 = 'fail!') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.