Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function with unknown number of parameters Python

I am trying to read in a Fuzzy plain text rule and pass the parameters to a SciKit-Fuzzy function call to create fuzzy rules. For example, if I read in this text rule:

IF service IS poor OR food IS rancid THEN tip IS cheap

Then the function call will be:

ctrl.Rule(service ['poor'] | food ['rancid '], tip ['cheap'])

If text rule is:

IF service IS good THEN tip IS average; 

Then the function call will be:

ctrl.Rule(service ['good '] , tip ['average'])

Since each rule can have unlimited number of input variables, e.g. the user can also say:

IF service IS good AND food IS good AND mood IS happy THEN tip IS high

which contains 3 inputs variables service['good'],food['good'],and mood['happy'], and 1 output variable tip['high']. I can't think of a way to automatically read in the text rule and convert it to a function call, do you have any idea or suggestion to achieve this goal? Any help will be appreciated. Thanks.

like image 680
GHWHG Avatar asked Aug 16 '20 20:08

GHWHG


People also ask

How do you accept an unknown number of parameters in Python?

Well, using *args in a function is Python's way to tell that this one will: Accept an arbitrary number of arguments.

Does Python support unknown number of parameters in functions?

Python lets us define a function that handles an unknown and unlimited number of argument values.

Can a function have variable No of parameters in Python?

Yes. You can use *args as a non-keyword argument. You will then be able to pass any number of arguments. As you can see, Python will unpack the arguments as a single tuple with all the arguments.

How do you define a function that takes variable number of arguments?

To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types.


1 Answers

In python, we can pass an unknown amount of arguments into the function using asterisk notation.

Let's try to create a function sum_up() with an unknown number of arguments.

def sum_up(*args):
    s = 0
    for i in args:
        s += i
    return s

As you see, an argument with an asterisk before will collect all arguments given to this function inside a tuple called args.

We can call this function that way:

sum_up(5, 4, 6)  # Gives 15

But if we want to sum up elements of a list and we need to pass it into the function as arguments...

We can try the following:

l = [5, 4, 6]
sum_up(l)

This won't give an effect we need: args of sum_up will look like ([5, 4, 6],). To do what we want, we need to put an asterisk before the argument we're passing:

sum_up(*l)  # Becomes sum_up(5, 4, 6)

All you need to do is collect all arguments you want to pass in a list and then put an asterisk before this list passed as an argument inside a call:

args = [service ['good '] , tip ['average']]
ctrl.Rule(*args)
like image 155
AlanTheKnight Avatar answered Sep 23 '22 06:09

AlanTheKnight