Search code examples
pythonalgorithmpython-2.7structurepretty-print

Python 2.7, smart conditioning


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.


Solution

  • 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.