AutomaticStatic
AutomaticStatic

Reputation: 1739

Python: Conditionals for optional function arguments

Let's say I have a function that accepts 3 optional arguments:

def function(arg1=0, arg2=0, arg3=0)

What is the cleanest way to handle conditionals within the function depending on which argument is passed?

Right now all I have is:

def function(arg1=0, arg2=0, arg3=0)
    if arg1 !=0 and arg2 !=0 and arg3 != 0:
        # do stuff with all three args
    elif arg1 !=0 and arg2 != 0:
        # do stuff with arg1 and arg2, etc...

To expand upon this, what if my function can take 5 arguments? Doing conditionals for all possible combinations seems like a drag. Can I not do something else to check which arguments have been passed to the function?

UPDATE: Based on some feedback I guess I'll just explain in real terms what I'm doing. I need to estimate someone's age based on when they graduated from school (high school, college, graduate program, etc). I may have multiple years to go on, and in fact I may have multiple years for each of high school, college, etc.

So, an example might be:

def approx_age(highSchool=0, college=0):
    this_year = date.today().year
    if (highSchool != 0) and (college != 0):
        hs_age = (this_year - highSchool) + 18
        college_age = (this_year - college) + 21
        age_diff = abs(hs_age - college_age)
        if age_diff == 0:
            return hs_age
        elif return (hs_age + college_age)/2
    elif highSchool != 0:
        hs_age = (this_year - highSchool) + 18
        return hs_age
    elif college != 0:
        college_age = (this_year - college) + 21
        return college_age

Things are only going to get more complicated from here...

Upvotes: 0

Views: 1311

Answers (4)

Tanveer Alam
Tanveer Alam

Reputation: 5275

def function(arg1=None, arg2=None, arg3=None)
    if (arg1 and arg2 and arg3):
        # do stuff with all three args
    if (arg1 and arg2):
        # do stuff with arg1 and arg2, etc...

I think None would work better in your case.

>>> if (1 and 1 and 1):
...     print True
... 
True
>>> if (0 and 0 and 0):
...     print True
...
... 
>>> if (None and None and None):
...     print True
...

So it you will get all 3 args in first condition then only it will get inside of your if scope.

Same for case second it will get inside of if scope if both arg1 and arg2 are not None or 0.

Upvotes: 0

Erin Call
Erin Call

Reputation: 1784

I'm not certain since I don't know what your function is doing with all these arguments, but it seems like you should use **kwargs:

def function(**kwargs):
    for key, value in kwargs.iteritems():
        do_the_thing()

The ** syntax means your function will accept any and all keyword arguments, and stick them in a dict. kwargs is a common name to give that dict. You can also use *args to get an array of positional arguments.

Oh also--if you do end up checking truthiness of the arguments, please consider using all:

if all([arg1, arg2, arg3]):

Upvotes: 0

Vishnu Upadhyay
Vishnu Upadhyay

Reputation: 5061

why to check the argumnet with value 0 and use of elif is pythonic. conditional statement check statement Truth value if it is True then condition proceed else switch for next condition.

and

In [112]: bool(0)
Out[112]: False

In [113]: bool(None)
Out[113]: False

so if argument value is 0 you don't need to match it with 0 python selfly illustrate it.

def function(arg1=0, arg2=0, arg3=0)
    if arg1 and arg2 and arg3:
        # do stuff with all three args

    elif arg1 and arg2:
        # do stuff with arg1 and arg2, etc...
    ..
    ..

    else:
        #do something

or for None too:

def function(arg1=None, arg2=None, arg3=None)
        if arg1 and arg2 and arg3:
            # do stuff with all three args

Upvotes: 2

Piyush Deshmukh
Piyush Deshmukh

Reputation: 529

I guess you can boil it down to this using basic math

def function(arg1=0, arg2=0, arg3=0):
    if arg1 * arg2 * arg3 != 0:
        # do stuff with all three args
    if arg1 * arg2 != 0:
        # do stuff with arg1 and arg2, etc...

Upvotes: 0

Related Questions