regarding memoize function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ankitks.mital@gmail.com

    regarding memoize function

    I saw example of memoize function...here is snippet

    def memoize(fn, slot):
    def memoized_fn(obj , *args):
    if hasattr(obj, slot):
    return getattr(obj, slot)
    else:
    val = fn(obj, *args)
    setattr(obj, slot, val)
    return val
    return memoized_fn


    and I am really clueless, about what it does. I know in general we try
    to keep computed values for future usage. But I am having hard-time
    visualizing it.
    What is obj here? and what does *args means?
    Thanks
  • Dan Bishop

    #2
    Re: regarding memoize function

    On Apr 3, 6:33 pm, ankitks.mi...@g mail.com wrote:
    I saw example of memoize function...here is snippet
    >
    def memoize(fn, slot):
    def memoized_fn(obj , *args):
    if hasattr(obj, slot):
    return getattr(obj, slot)
    else:
    val = fn(obj, *args)
    setattr(obj, slot, val)
    return val
    return memoized_fn
    >
    and I am really clueless, about what it does. I know in general we try
    to keep computed values for future usage. But I am having hard-time
    visualizing it.
    What is obj here? and what does *args means?
    *args is Python's syntax for variadic functions.

    Comment

    • Gabriel Genellina

      #3
      Re: regarding memoize function

      En Thu, 03 Apr 2008 21:21:11 -0300, Dan Bishop <danb_83@yahoo. com>
      escribió:
      On Apr 3, 6:33 pm, ankitks.mi...@g mail.com wrote:
      >I saw example of memoize function...here is snippet
      >>
      >def memoize(fn, slot):
      > def memoized_fn(obj , *args):
      > if hasattr(obj, slot):
      > return getattr(obj, slot)
      > else:
      > val = fn(obj, *args)
      > setattr(obj, slot, val)
      > return val
      > return memoized_fn
      >>
      >and I am really clueless, about what it does. I know in general we try
      >to keep computed values for future usage. But I am having hard-time
      >visualizing it.
      >What is obj here? and what does *args means?
      >
      *args is Python's syntax for variadic functions.
      In case the strange name gives you nothing, see section 4.7 in the
      Tutorial [1]
      For a much simpler implementation, see this FAQ entry [2]

      [1] http://docs.python.org/tut/node6.htm...00000000000000
      [2]
      Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...


      --
      Gabriel Genellina

      Comment

      Working...