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


Groups > comp.lang.python > #97385

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

Date 2015-10-03 20:09 +0200
From Ervin Hegedüs <airween@gmail.com>
Subject Re: function code snippet that has function calls I have never seen before. How does it work.
References <ae836f80-185b-45ad-81b0-a3644a0e649e@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.371.1443895792.28679.python-list@python.org> (permalink)

Show all headers | View raw


hi,

On Sat, Oct 03, 2015 at 10:40:57AM -0700, Ronald Cosentino wrote:
> 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.

it's simple:

- there is a "composition of functions", generally
  f(g()) (function in argument list of another function)
- first, Python evaulates the arguments first, from left to right 
- in this point, you'll get -1 for 2nd arg, and 1 for 3rd arg
- then your funcA() will be called with these arguents:
  4, -1, 1
- funcA() calculates this:
  (x+y)*z, in this case (4+(-1))*1


which is 3...


a.


-- 
I � UTF-8

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