Need help to understand why a function equate to lesser variables

Hello I got this script and I am trying to understand how it works. There’s a function named “eval” with mutiple variables. But somewhere down the script the “eval” was equated to 2 variables only. Have tried to google it and did some experiment simulation but difficult. Hope someone here can help me understand. Thanks

###### Main Function #######

def eval(Cb1, Vb1, kb, pb_pen, ImpDepth, A_Area, Lbact_x, Lbact_y, runtype, g_write, file_write):




##### Main Calculation ######

if loop_bb==True:
    
    if optimization_parameter_stddev == True:
        for bb1 in range(0, int(kb_val_max * 10)):
            b = k1 * 0.1
            std_deviation, quotient = eval(Cb1, Vb1, kb, pb_pen, IbmpDepth, Abctive_Area, Lbact_x, Lbact_y, 0, Graph, filewrite)
            if kb == 0:
                stddev_low = std_deviation
                k_best = kb

You should avoid of doing two things: (1) posting screenshots instead of copy-paste the relevant code and (2) using built-in function names as names of your own functions/variables.

Hello,

please do not send text in pictures next time. It is hard to work with pictures.

The statement is not “equating” the function. It is calling the function and assigns the result to the variable names on the left side of the = character. Also your use of the word “methods” does not make sense to me.

The assignment statement you are showing is expecting the function to return a tuple with two items. What you are looking for is unpacking.

Note that the choice of the function name eval is poor. It shades the built-in function eval.

Thank you so much, I have corrected it right away…thanks
The “eval” is a function…thanks

Thank you…I edited my statements…thanks

Hello There, have tried check the link but am still not quite understand because their samples dont have functions…Can you please help make a sample code with function being called…I am python beginner…thank you.

Example without a function:

tuple_of_numbers = 1, 2

# Here the right side of the assignment statement is a tuple with value (1, 2).
number1, number2 = tuple_of_numbers
print(number1)
print(number2)
1
2

Similar example with a function:

def return_tuple_of_numbers():
    return 1, 2

# Here the right side of the assignment statement evaluates to a tuple (1, 2) too.
number1, number2 = return_tuple_of_numbers()
print(number1)
print(number2)
1
2

It would be probably best for you to go through a tutorial which would explain basic concepts used in Python.

Thank you for the example…Can you please make 2nd example code but this time showing like below, because the original code I got left side have 2 variables than function with more than 2 variables:

def func1(a,b,c):

x, y = func1(a,b,c)

Thanks in advance

As vbrozik said, the function on the right-hand side returns a tuple of 2 values, which are assigned to 2 variables on the left-hand side.

The number of arguments (variables) that are passed to the function is irrelevant.

3 Likes

Thanks for reply and backing up vbrozik…But why this code calling a function with many variables, the left side only have 2 variables…Please help make new sample with multiple variables in the function than the left side…sorry that i cant understand faster, pls help…thanks

std_deviation, quotient = eval(Cb1, Vb1, kb, pb_pen, IbmpDepth, Abctive_Area, Lbact_x, Lbact_y, 0, Graph, filewrite)

It sounds like you need to spend some time with basic tutorials on functions. There is literally no relationship between the number of parameters (inputs) and return values (outputs).

Ok, please give me hint what keywords or link where i can study this.
The sample given having empty variables → def return_tuple_of_numbers():
All i want is to please give me a sample like → def return_tuple_of_numbers(x,y,x):
Looking forward for your understanding…thanks

Please do the Python tutorial.

There is no connection between the number of input arguments of a function and the number of output values it returns. They are completely independent.

Hello and good day to all…yes I am begineer, am still hoping someone can give a sample as I requested?

def func1(a, b, c, d, e):
main():
   x, y = func1(a, b, c, d, e)

Regardless if it is independent or not, please share some example…Thanks

Sorry for the catch up, i found this variabes have different types as well.

image

Yea, of course the types can be different because as before, the parameters and return values are independent. I think you’re overthinking this and getting hung up on some connection you imagine between the two, when there just isn’t one.

By Rhett via Discussions on Python.org at 05Sep2022 03:17:

Hello and good day to all…yes I am begineer, am still hoping someone
can give a sample as I requested?

Try this:

 def sum3(a, b, c):
     return a + b + c

 main():
     total = sum3(1, 2, 3)
     print("total =", total)

Here’s a function returning a single value, from 3 inputs. It isn’t
fundamentally very different from your eval() example, which also has
many input values but returns a pair of output values. Here’s a function
returning a pair:

 def min_max(a, b, c):
     return min(a, b, c), max(a, b, c)

 main():
     small, large = min_max(1, 2, 3)
     print("smallest =", small, "largest =", large)

I think you’re conflating the returned value with the source value, as
others have mentioned. Let’s try a different approach:

Python functions always return exactly one value.

From that it follows that since you can pass many values as arguments to
a function call, you’re always getting just one value back and so there
can be no inherent correspondence between the number of input values and
the single return value.

Now, that might sound like a fiction, so let me elaborate.

The “returning one value” is pretty obvious with the sum3() example
above, but less obvious with min_max(). The min_max() function
returns one value, it is just that that value is a 2-tuple. I can do
this quite validly:

 mm = min_max(1, 2, 3)

Watch:

 >>> def min_max(a, b, c):
 ...     return min(a, b, c), max(a, b, c)
 ...
 >>> mm = min_max(1, 2, 3)
 >>> type(mm)
 <class 'tuple'>
 >>> print(len(mm))
 2
 >>> print(mm)
 (1, 3)
 >>> small, large = mm
 >>> type(small)
 <class 'int'>
 >>> print(small)
 1

What you’re seeing with small, large = mm is what we call an
unpacking assignment in Python. If there are multiple variable names
on the left of the assignment, the single value on the right of the
assignment is iterated, and the values from the iteration assigned to
each of the variables on the left. And they have to match up!

Observe:

 >>> a, = [1]
 >>> a
 1

Python iterated over a 1-element list, and stuck its values into a
etc. But there’s just one element and just one variable. That comma is
important, syntacticly.

More common:

 >>> a, b = 1, 2
 >>> print(a)
 1
 >>> print(b)
 2

The right hand is a still a single value, a 2-tuple. More overtly:

 >>> a, b = (1, 2)
 >>> print(a)
 1
 >>> print(b)
 2

Still a single 2-tuple, but the brackets make it more obvious. But the
brackets are not part of the tuple syntax, they’re just grouping, the
same as here:

 x = 3 * (5 + 7)

The brackets do grouping - they are not part of the addition +
operation.

You can put any iterable value on the right. Here’s a a list, like the
a, example:

 >>> a, b = [1, 2]
 >>> print(a)
 1
 >>> print(b)
 2

Still a single value, a single list with 2 elements inside it.

Returning to functions:

 >>> mm = min_max(1, 2, 3)
 >>> type(mm)
 <class 'tuple'>
 >>> print(mm)
 (1, 3)
 >>> small, large = mm

The example which confused you initially was the same, just folded up
into one line:

 >>> small, large = min_max(1, 2, 3)

Python is just unpacking the 2-tuple from min_max() into the 2
variables on the left of the assignment.

Does this clarify what’s happening?

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes

Small nit: Python variables do not have types, they are all references to objects (an int, a str, etc etc). The objects have types.

Cheers,
Cameron Simpson

3 Likes

@cameron You’re a great person, thank you for not giving up, you really hit what I want to see…All the best to you. Thanks again.