Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97384

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

From "Joseph Lee" <joseph.lee22590@gmail.com>
References <ae836f80-185b-45ad-81b0-a3644a0e649e@googlegroups.com>
Subject RE: function code snippet that has function calls I have never seen before. How does it work.
Date 2015-10-03 11:03 -0700
Newsgroups comp.lang.python
Message-ID <mailman.370.1443895403.28679.python-list@python.org> (permalink)

Show all headers | View raw


Hi Ronald,
Answers inline.

-----Original Message-----
From: Python-list
[mailto:python-list-bounces+joseph.lee22590=gmail.com@python.org] On Behalf
Of Ronald Cosentino
Sent: Saturday, October 3, 2015 10:41 AM
To: python-list@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

function code snippet that has function calls I have never seen before. How does it work. Ronald Cosentino <ronjc.2001@gmail.com> - 2015-10-03 10:40 -0700
  RE: function code snippet that has function calls I have never seen before. How does it work. "Joseph Lee" <joseph.lee22590@gmail.com> - 2015-10-03 11:03 -0700
  Re: function code snippet that has function calls I have never seen before. How does it work. Ervin Hegedüs <airween@gmail.com> - 2015-10-03 20:09 +0200
  Re: function code snippet that has function calls I have never seen before. How does it work. Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-03 19:59 +0000
  Re: function code snippet that has function calls I have never seen before. How does it work. Steven D'Aprano <steve@pearwood.info> - 2015-10-05 00:42 +1100

csiph-web