0

I would like to call a function with dynamic parameter input, I am not sure if it is possible, any suggestion is welcome.

The scenario is like below:

I have a function with 5 parameters like this

function(p1=v1, p2=v2, p3=v3, p4=v4,p5=v5)

v1-v5 are saved in an excel file and can be empty, if the value for a parameter is none(nothing entered in the excel), the parameter should not appear when call the function.

For example, if v2=None, when I call the function, it will look like function(p1=v1, p3=v3, p4=v4,p5=v5).

Is it possible by checking the value of each parameter that I can call the function dynamically with parameters that have meaningful value?

1 Answer 1

1

Perhaps you can do it like this;

When you declare your function you can set a default value and handle that case

def myFunction(p1=None, p2=None):
   // do some work and check if values exist

Then call your function like below:

result = myFunction(p1=v1) 

Notice how we do not have to enter a value for p2. If no value is declared the default value of None will be set.


As an aside depending on your data/usecase you might also want to consider using a different data structure like an array/list

def myFunction(myList):
    // loop through myList and do something with the data
    
aList = [v1,v3,v4,v5]
result = myFunction(aList)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.