0

I have defined three functions.

def evaluate1(a, b):
 pass
def evaluate2(a, b):
 pass
def evaluate3(a, b, c):
 pass

What I want to do use a pointer to record which evaluate function I will use depending on the test inputs. The logic is as shown follows:

def test(a, b, c, d):
 # let evaluate_function records which evaluate function I will use
 if c > 1:
 evaluate_function = evaluate3 # not sure
 else:
 if d:
 evaluate_function = evaluate1
 else:
 evaluate_function = evaluate2
 # execute the evaluate function
 evaluate_function(a, b, ?)

However, since evaluate3 has different arguments from evaluate1 and evaluate3. How should I do? Thanks!

asked Apr 4, 2019 at 7:28
6
  • use **kwargs that will help you with dynamic signatures. Commented Apr 4, 2019 at 7:31
  • Must they have different arguments? If yes, could you use e.g. functools.partial or a lambda to wrap the one that takes three arguments such that the resulting evaluate_function always takes two arguments? If no, it would be easier if they didn't. Commented Apr 4, 2019 at 7:31
  • Use *args in the definitions Commented Apr 4, 2019 at 7:33
  • You can use default param in function declaration in c of evaluate3 if it can Commented Apr 4, 2019 at 7:33
  • Is the code in all three functions different? Commented Apr 4, 2019 at 7:45

2 Answers 2

1

You have come up with a good idea of using a 'function pointer' to select the function. But since you know which function you are selecting at the time, you could also bind up the params:

def test(a, b, c, d):
 # let evaluate_function records which evaluate function I will use
 if c > 1:
 evaluate_function = evaluate3 # not sure
 params = a,b,d
 else:
 if d:
 evaluate_function = evaluate1
 params = a,b
 else:
 evaluate_function = evaluate2
 params = a,c
 # execute the evaluate function
 evaluate_function(*params)

I'll leave it to you to properly select the params.

answered Apr 4, 2019 at 7:34
Sign up to request clarification or add additional context in comments.

Comments

1

Why not just call the evaluate functions directly instead of assigning them to a function as so. Makes it more readable

def evaluate1(a, b):
 print('evaluate1')
def evaluate2(a, b):
 print('evaluate2')
def evaluate3(a, b, c):
 print('evaluate3')
def test(a, b, c=None, d=None):
 # let evaluate_function records which evaluate function I will use
 if c and c > 1:
 evaluate3(a, b, c)
 else:
 if d:
 evaluate1(a, b)
 else:
 evaluate2(a, c)
test(1,2,c=0.1,d=1)
#evaluate1
test(1,2)
#evaluate2
test(1,2,3)
#evaluate3
answered Apr 4, 2019 at 7:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.