-1

Here is my case: I got to booleans a and b and I have function eatAB() which can either eat a or b or none.

Here is my problem: eatAB() have to be called once and I want it 'smart-and-pretty'. I could do something like this:

if not a and not b:
    eatAB()
elif a and not b:
    eatAB(a=a)
elif not a and b:
    eatAB(b=b)
else:
    eatAB(a,b)

But For me this one sucks kinda) Is there a prettier or better or smarter or other way to do it? Appreciate your time.

ChynaJake
  • 51
  • 1
  • 6
  • 1
    how does your function 'eat' the booleans?? Show the code for `eatAB` and your desired final output. It might be this logic can go *inside* the function much more neatly – Chris_Rands Nov 23 '17 at 12:56
  • 2
    I'm assuming since you're happy to use the same function for all cases, that it checks whether a or b are None to do it's logic. If so, pass them in as `eatAB(a if a else None, b if b else None)` and get rid of all the conditions. –  Nov 23 '17 at 12:59
  • 2
    @Bilkokuya The `if` checks can also be moved inside the function – Chris_Rands Nov 23 '17 at 13:00

1 Answers1

0

This post is split into two, the top is an updated answer based on new information from the OP - regarding that eatAB() is not allowed to be, or able to modified. The second answer is the original answer to how you'd solve this if you have access to modify the function itself.


Updated Answer (where you lack access/permission to modify function)

As you do not have access to change the function internally, but you do know it's signature eatAB(a=None,b=None) we want to follow this logic (from the question):

  • If the value we are passing in is truthy (e.g. True), we want to pass the value
  • If the value is not true, we want to use the default value for the parameter, which is None

This can be easily accomplished using the following expression:

value if condition else otherValue

Which when used when calling the function gives the following:

a = False
b = True
eatAB(a if a else None, b if b else None)
# will be the same as calling eatAB(None, True) or eatAB(b=True)

Of course, if the values of a and b come from a condition themselves, you can just use that condition. For example:

eatAB(someValue if "a" in myDictionary else None, someOtherValue if "b" in myDictionary else None)

Original answer (where you have access to modify the function):

Not knowing what exactly eatAB() does or it's exact signature, the best I can recommend is the following. I'm sure you can adapt this however you need.

The main idea is to move that logic into eatAB() as it is the responsibility of the function and not the calling code. Explanation is in the comments:

# for parameters a and b to be optional as you have shown, they must have a default value
# While it's standard to use None to denote the parameter is optional, the use case shown in the question has default values where a or b are False - so we will use that here.
def eatAB(a=False, b=False):
    # check if the parameters are truthy (e.g. True), in which case you would have passed them in as shown in the question.
    if a:
        # do some logic here for when a was a truthy value
    if b:
        # do some logic here for when b was a truthy value
    # what exactly the eatAB function I cannot guess, but using this setup you will have the same logic as wanted in the question - without the confusing conditional block each time you call it.

# function can then be called easily, there is no need for checking parameters
eatAB(someValue, someOtherValue)

Thanks to Chris_Rands for the improved suggestion.

  • Hey, I guess I need to provide a little more details: – ChynaJake Nov 24 '17 at 05:47
  • Actual task is this: we have dictionary dictionary = {}, which may or may not contain key 'a' or 'b'. we are not allowed to modify 'eatAB()' inside of it function 'eatAB()' is like this: 'eatAB(a=None, b=None): ....'. And 'eatAB()' takes values of "dictionary['a']" and "dictionary['b']", we have to just make sure it doesn't raise KeyError and eatAB() don't have to be called more than once. So the problem is checking key a or b in dictionary and then passing it inside eatAB(). Sorry for not showing full question before, thought can make it work with booleans alone. Thank you – ChynaJake Nov 24 '17 at 05:58
  • @ChynaJake In future, please clarify any and all contraints in the question - otherwise you waste the answerer's time and effort trying to help. I have updated the answer to include the new information you've added. –  Nov 24 '17 at 11:00