Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:50:49+00:00 2026-06-01T20:50:49+00:00

There is a tutorial at http://pythonprogramming.jottit.com/functional_programming and it gives an example how to use

  • 0

There is a tutorial at http://pythonprogramming.jottit.com/functional_programming and it gives an example how to use higher order functions to return functions:

def trace(f):
    f.indent = 0
    def g(x):
        print '|  ' * f.indent + '|--', f.__name__, x
        f.indent += 1
        value = f(x)
        print '|  ' * f.indent + '|--', 'return', repr(value)
        f.indent -= 1
        return value
    return g

and

def memoize(f):
    cache = {}
    def g(x):
        if x not in cache:
            cache[x] = f(x)
        return cache[x]
    return g

but I don’t get how it’s able to assign two functions on the same variable on the statements:

fib = trace(fib)
fib = memoize(fib)
print fib(4)

both trace and memoize seem to have effect on the last call. Why is that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:50:50+00:00Added an answer on June 1, 2026 at 8:50 pm

    Both trace() and memoize() create a new function object and return it to you.

    In each case, the new function object “wraps” the old function object, so the original function is not lost.

    Using my amazing ASCII art skills, here is a diagram:

    f(x)   # this is your original function
    
    trace(f(x))   # trace "wraps" it and returns a wrapped object
    
    memoize(trace(f(x)))   # memoize "wraps" it and returns a new wrapped function object
    

    We start out with a function object bound to the name fib.

    Then we call trace(fib) which creates a new function object. When it is first created, its name is g but we then bind it to the name fib. Try printing fib.__name__.

    Then we call memoize(fib) which creates a new function object. Again it’s first created with the name of g but then bound to the name fib.

    Remember, in Python everything is an object, and objects can exist with no name, with one name, or with many names. In this case, we keep re-using the name fib but we keep re-binding it with different function objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to include Core Location and I'm trying to follow this tutorial: http://www.mobileorchard.com/hello-there-a-corelocation-tutorial/
I was reading the Gallery View tutorial : http://developer.android.com/resources/tutorials/views/hello-gallery.html . Is there anyway I
I'm following Scott Guthries MVC tutorial ( http://nerddinnerbook.s3.amazonaws.com/Part6.htm ) and there's something I don't
I am using iOS SDK 4.2 and followed this tutorial: http://mobileorchard.com/hello-there-a-corelocation-tutorial/ , and got
I am following this tutorial http://wicket.wordpress.com/2010/01/08/template-for-building-authenticated-webapplication/ in order to learn how to make login
I have a problem with following tutorial: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/ In faces-context file you have to
like in title:) I wrote from tutorial: http://msdn.microsoft.com/en-us/wp7trainingcourse_yourfirstwp7applab_topic3 does everything like there. my code:
There is a tutorial here: http://perishablepress.com/slide-fade-content/ and the code it provides is: $(document).ready(function(){ //
I'm using this tutorial: http://www.objectdb.com/tutorial/jpa/eclipse/ee/ejb I keep seeing words prefixed with @. Is there
I downloaded Ray Wenderlich's simple contact listener from this tutorial: http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone Currently I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.