function code snippet that has function calls I have never seen before. How does it work.

Joseph Lee joseph.lee22590 at gmail.com
Sat Oct 3 14:03:07 EDT 2015


Hi Ronald,
Answers inline.

-----Original Message-----
From: Python-list
[mailto:python-list-bounces+joseph.lee22590=gmail.com at python.org] On Behalf
Of Ronald Cosentino
Sent: Saturday, October 3, 2015 10:41 AM
To: python-list at python.org
Subject: function code snippet that has function calls I have never seen
before. How does it work.

def funA(x,y,z):
    return (x+y) * z
def funB(x,y):
    return(x-y)
print(funA(4,funB(2,3), funB(3,2)))

the answer is 3. I don't know how it works.

JL: Okay, let's step through the print routine.

1. Before print does its job, the function result will be gathered.
2. The argument to print is result of funA, which itself takes result of two
calls to FunB.
3. First, results of funB calls will be gathered (subtracting 3 from 2 and 2
from 3, respectively). So it becomes:
print(funA(4, (2-3), (3-2)))
4. Then funA will continue to perform its job, returning the result for
output. Thus the final expression that print will print is:
print((4+-1) * 1)

When looking at a function that takes result of another function, it is
important to look at what the inner callee does (in this case, look at what
funB does first).
Cheers,
Joseph



-- 
https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list