By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

regarding memoize function

P: n/a
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
Apr 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Apr 3, 6:33 pm, ankitks.mi...@gmail.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.
Apr 4 '08 #2

P: n/a
En Thu, 03 Apr 2008 21:21:11 -0300, Dan Bishop <da*****@yahoo.com>
escribió:
On Apr 3, 6:33 pm, ankitks.mi...@gmail.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]
http://www.python.org/doc/faq/genera...etween-objects

--
Gabriel Genellina

Apr 4 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.