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

  • SEARCH
  • Home
  • 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 6953335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:27:55+00:00 2026-05-27T14:27:55+00:00

I got the following code snippet from Peter Norvig’s website; it’s a decorator to

  • 0

I got the following code snippet from Peter Norvig’s website; it’s a decorator to enable memoization on function calls (caching prior calls to the function to change an exponential recursion into a simple dynamic program).

def memo(f):
    table = {}
    def fmemo(*args):
        if args not in table:
            table[args] = f(*args)
        return table[args]
    fmemo.memo = table
    return fmemo

The code works fine, but I’m wondering why the second to last line is necessary. This is clearly a gap in my knowledge of Python, but removing the line and running a simple fibonacci function, it still seems to work. Does this have to do with memoizing multiple functions simultaneously? Why would the member variable of fmemo be called memo (assuming it’s not an awkward coincidence)?

Thanks!

  • 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-05-27T14:27:55+00:00Added an answer on May 27, 2026 at 2:27 pm

    Since functions are objects just like anything else, you can set attributes on them. See:

    >>> def foo(): pass
    >>> foo.x = 1
    >>> foo.x
    1
    

    The second-to-line sets the internal cache of values as an attribute on the function object, thus exposing it. This means that you can take a memoised function and fiddle with its cache as you will, without having to call it. This might be convenient.


    Example:

    >>> @memo
    ... def id(x): return x
    >>> id(1)
    1
    >>> id(2)
    2
    >>> id.memo
    {(2,): 2, (1,): 1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive got the following code which I hacked together from website examples. It works
I've got the following code snippet. function addLinks () { for (var i=0, link;
The following code snippet prints the Pascals triangle,I have got this snippet from the
I have got the following code from here to read an Excel file using
I've got the following code from another question on SO to track the changing
I'm programming C++ on Ubuntu, using QDBus and I've got the following code snippet:
Working through Pro ASP.NET MVC book and I got the following code snippet that
I've got the following code snippet that I'm thinking of refactoring to a more
I got the following code snippet referring an example in Chapter 6 of Programming
I got following code in an XHTML-document and every browser saysit is malformed :(

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.