2004 example, passing function error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • globalrev

    2004 example, passing function error


    >>>
    >>>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(sq r, cube, 2)
    64
    >>>

    but i get:
    >>compose(sqr , cube, 2)
    Traceback (most recent call last):
    File "<pyshell#5 6>", 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??
  • Carsten Haese

    #2
    Re: 2004 example, passing function error

    globalrev wrote:
    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??
    It looks like it's copy/pasted from a shell, but it's not. No past or
    present Python interpreter could produce an interactive session like
    that under any circumstances. My guess is that instead of copy/pasting,
    the author simply retyped the session and made a mistake in doing so.

    --
    Carsten Haese

    Comment

    • alex23

      #3
      Re: 2004 example, passing function error

      On May 14, 11:40 am, globalrev <skanem...@yaho o.sewrote:
      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??
      Adding the value argument (x) to the compose function really limits
      its usefulness as well. Given the author was talking about functions
      as first class objects, it makes more sense for compose to return a
      function, rather than to perform several function calls and return a
      value.

      Here, compose creates a new function that performs first 'f' then 'g'
      on a value:
      >>def compose(f, g):
      .... def _compose(x):
      .... return f(g(x))
      .... return _compose
      ....
      >>compose(sqr , cube)
      <function _compose at 0x00B7BEB0>
      >>compose(sqr , cube)(2)
      64

      The advantage being you can easily create function chains this way:
      >>sqrcube = compose(sqr, cube)
      >>sqrcube(2)
      64

      And because they're functions, you can use them with compose to
      construct more complex functions:
      >>cubesqrcube = compose(cube, sqrcube)
      >>cubesqrcube(2 )
      262144

      Just to show they're functionally the same:
      >>cubesqrcube(2 ) == cube(sqrcube(2) ) == cube(sqr(cube(2 )))
      True

      I like this approach because it separates the function composition
      from its execution. But you can also achieve much the same by using
      functools.parti al (in 2.5+):
      >>def compose(f, g, x): return f(g(x))
      ....
      >>compose(sqr , cube, 2)
      64
      >>from functools import partial
      >>sqrcube = partial(compose , sqr, cube)
      >>sqrcube(2)
      64
      >>cubesqrcube = partial(compose , cube, sqrcube)
      >>cubesqrcube(2 )
      262144

      Hope this helps.

      - alex23

      Comment

      Working...