0

I am defining a function which needs to take a user defined function user_function(a,b,possibly_lots_more_arguments). The problem is possibly_lots_more_arguments may be empty. I see two ways to handle this, and I'm wondering if one is "better" than the other.

Here's a fairly simple example. f handles this by defining a function that tests whether there are extra arguments. g handles this by creating a longer list. The func1 and func2 functions are obviously toy examples of a user-defined function.

def f(user_function, parameters = None):

    def my_user_function(alpha, beta, my_parameters):
        if my_parameters == None:
            return user_function(alpha, beta)
        else:
            return user_function(alpha, beta, *my_parameters)

    for a in range(4):
        for b in range(3):
            if my_user_function(a,b,parameters):
                print "it's True"
            else:
                print "it's False"


def g(user_function, parameters = []):
    for a in range(4):
        for b in range(3):
            L=[a,b]+list(parameters) #parameters could be a tuple
            if (user_function(*L)):
                print "it's True"
            else:
                print "it's False"

def func1(a,b,c):
    return a+b<c

def func2(a,b):
    return a+b<4


print 'f(func1)'
f(func1,[4])
print 'g(func1)'
g(func1,[4])

print 'f(func2)'
f(func2)
print 'g(func2)'
g(func2)

I feel like g is better, but I may need to call the user function several times and this could get ugly.

I've never had to write transparent code except for making sure I can understand it a year later. The code I'm doing here is likely to be used by others. Is one of these inherently better either for performance or readability reasons, or is another option even better?

Joel
  • 22,598
  • 6
  • 69
  • 93
  • why not use *args for a variable number of positional arguments? http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters – Garrett R Feb 20 '16 at 00:37
  • 1
    What @GarrettR said. Also `foo is None`, not `foo == None`, for testing for `None`. http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python – gil Feb 20 '16 at 00:45
  • @GarrettR Really I have several user defined functions, so I can't make my function into something like `f(userfunc, *parameters)` - my function needs to be `f(userf1, userf2, parameters1=[], parameters2=[])` . I mistakenly thought that calling `userf1(*parameters1)` failed if it was empty. – Joel Feb 20 '16 at 01:48

1 Answers1

0

Did you just not realize that *args works fine with an empty args?

def h(user_function, parameters=()):
    for a in range(4):
        for b in range(3):
            if (user_function(a, b, *parameters)):
                print "it's True"
            else:
                print "it's False"
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • I did not realize that --- In fact, I thought I tested it and was surprised it didn't work – Joel Feb 20 '16 at 01:31