2004 example, passing function error

globalrev skanemupp at yahoo.se
Tue May 13 21:40:36 EDT 2008


http://linuxgazette.net/109/pramode.html

>>>
>>>def sqr(x): return x*x
...
>>>def cube(x): return x*x*x
...
>>>sqr
<function sqr at 0x402ba10c>
>>>a = [sqr, cube]
>>>a[0](2)
>>>def compose(f, g): return f(g(x))
...
>>>compose(sqr, cube, 2)
64
>>>


but i get:

>>> compose(sqr, cube, 2)

Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    compose(sqr, cube, 2)
TypeError: compose() takes exactly 2 arguments (3 given)


this:

>>> def compose(f, g, x): return f(g(x))
>>> compose(sqr, cube, 2)
64

works though
. so just a mistake on his part? but it looks like he just copied his
shell...has there been a change since 2004 inr egards to how you can
pass functions as arguments to functions??



More information about the Python-list mailing list