Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97383 > unrolled thread
| Started by | Ronald Cosentino <ronjc.2001@gmail.com> |
|---|---|
| First post | 2015-10-03 10:40 -0700 |
| Last post | 2015-10-05 00:42 +1100 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Ronald Cosentino <ronjc.2001@gmail.com> |
|---|---|
| Date | 2015-10-03 10:40 -0700 |
| Subject | function code snippet that has function calls I have never seen before. How does it work. |
| Message-ID | <ae836f80-185b-45ad-81b0-a3644a0e649e@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | "Joseph Lee" <joseph.lee22590@gmail.com> |
|---|---|
| Date | 2015-10-03 11:03 -0700 |
| Message-ID | <mailman.370.1443895403.28679.python-list@python.org> |
| In reply to | #97383 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ervin Hegedüs <airween@gmail.com> |
|---|---|
| Date | 2015-10-03 20:09 +0200 |
| Message-ID | <mailman.371.1443895792.28679.python-list@python.org> |
| In reply to | #97383 |
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
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-10-03 19:59 +0000 |
| Message-ID | <mupc2b$ct3$1@dont-email.me> |
| In reply to | #97383 |
On Sat, 03 Oct 2015 10:40:57 -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.
def funA(x, y, z):
return (x+y) * z
def funB(x, y):
return (x-y)
# this line
# print(funA(4,funB(2,3), funB(3,2)))
# can be written as the following 4 lines:
a = funB(2, 3) # 2 - 3 -> -1
b = funB(3, 2) # 3 - 2 -> 1
c = funA(4, a, b) # (4 + -1) * 1 -> 3
print(c) # 3
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-10-05 00:42 +1100 |
| Message-ID | <56112cc8$0$1597$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #97383 |
On Sun, 4 Oct 2015 04:40 am, 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. Break it up and consider it a little at a time, starting with the three values given to funA: * calculate 4 (too easy, it's already done) * calculate funB(2, 3) => funB(2, 3) returns 2-3, which gives -1 * calculate funB(3,2) => funB(3, 2) returns 3-2, which gives 1 Then pass those three values to the funA function: * calculate funA(4, -1, 1) => which returns (4 + -1)*1, which gives 3 and finally pass that value to print: * print(3) which prints 3. -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web