0

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

5
  • You have conditions for all possible combinations, or just the number of arguments passed? Commented Dec 2, 2014 at 7:11
  • arg != 0 is equal to if arg in most cases, actually. Commented Dec 2, 2014 at 7:12
  • The second if clause will be true if the 1st if clause is true. Do you want that? Commented Dec 2, 2014 at 7:18
  • 2
    *args and **kwargs are the way to go. Commented Dec 2, 2014 at 7:18
  • @Sriram Sorry, I updated to change the second conditional to an elif. My mistake. Commented Dec 2, 2014 at 7:20

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Could you please see my addition to the OP regarding what to do if there are many potential arguments?
@AutomaticStatic 'Can I not do something else to check which arguments have been passed to the function?' i didn't get it mean what you want to do here?
0

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

1 Comment

That would work only if args will stay integers, tho.
0

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]):

4 Comments

How this "detects" if two arguments are 0, and which ones, or if 3 arguments are 0, etc?, or only one given, but which?
It doesn't; it operates on which arguments were actually passed by the caller.
Yep, and it seems that OP wants to know exactly which arguments were passed, if they are 0 or not, and do some some stuff depending on this.
It could be. I was reading those 0s as sigils for "no value was passed for this argument."
0
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.

Comments

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.