0

I'm pretty new to Python and have written a function that has 5 arguments:

def function(a,b,c,d=False,e=" "):

Argument d can either be true or false. If false, then the default value for e will be white space, and we wouldn't need to pass this into the function when calling it, if true, then we can either pass an argument to e, or leave it blank.

function(a,b,c,False) #Example 1: d==False 
function(a,b,c,True,'*') #Example 2: d==True, then pass value to e to override default

My issue, however, lies with the following example:

function(a,b,c,True) #Example 3: d==True but no value passed to e

If d is true and no value is passed to argument e, I want to change the default value of e to something else. I only want to do this in instances where d==True and e isn't passed. Basically, I want the default behaviour of argument e to be dependent on the boolean value of d:

def function(a,b,c,d=False,e=" " if d==False, '*' if d=True)

Basically, I want two default states for argument e, dependent on whether argument d is True or False.

Is this possible?

1 Answer 1

2

Use a None value for e and adjust if not passed

def function(a,b,c,d=False,e=None):
    if e is None:
        e="*" if d else ' '
    pass
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect! And so unbelievably simple! I knew there would be an efficient way to do this! Thanks!

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.