regarding memoize function

A

ankitks.mital

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
 
D

Dan Bishop

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.
 
A

ankitks.mital

En Thu, 03 Apr 2008 21:21:11 -0300, Dan Bishop <[email protected]>  
escribió:




*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.html#SECTION006700000000000000000
[2]  http://www.python.org/doc/faq/general/#why-are-default-values-shared-...

Thanks Gabriel and Dan,
But I am still confuse on...
what is obj?

Let say
def f(node): return max(node.path_cost+h(node), getattr(node, 'f', -
infinity))
f = memoize(f,'f')

what is this doing?
I am passing string 'f' as second argument? right? so evertime in
function memoize,
I am doing hasattr(obj, slot), I am saying hasattr(obj, 'f')?

I kindof understand that I am returning maximum of pre-computed
value(if there is already) vs. new calculation.
But syntax is throwing me off.
 
G

Gabriel Genellina

Thanks Gabriel and Dan,
But I am still confuse on...
what is obj?

Let say
def f(node): return max(node.path_cost+h(node), getattr(node, 'f', -
infinity))
f = memoize(f,'f')

what is this doing?
I am passing string 'f' as second argument? right? so evertime in
function memoize,
I am doing hasattr(obj, slot), I am saying hasattr(obj, 'f')?

I kindof understand that I am returning maximum of pre-computed
value(if there is already) vs. new calculation.
But syntax is throwing me off.

It *is* confusing. And a bit strange that it does not use the args
argument as a key (if it is true that f(*args) doesn't depend on args, why
using args in the first place?)

You may be calling f as f(a,b,c) or as a.f(b,c) - in both cases, obj is
`a`, the first argument (or "self" when used as a method)
An alternative version (with the same limitations with regard to *args)
but usable as a mehtod decorator:

from functools import wraps
def memoize(slot):
def decorator(fn, slot=slot):
@wraps(fn)
def inner(self, *args):
if hasattr(self, slot):
return getattr(self, slot)
else:
val = fn(self, *args)
setattr(self, slot, val)
return val
return inner
return decorator

class Foo(object):
def __init__(self, items):
self.items = tuple(items)

@memoize('max')
def hardtocompute(self):
return max(self.items)

a = Foo((10,20,30))
assert not hasattr(a,'max')
assert a.hardtocompute()==30
assert a.max==30
del a.items
assert a.hardtocompute()==30

There is an excelent article by Michele Simionato explaining decorators
with some useful recipes.
http://www.phyast.pitt.edu/~micheles/python/documentation.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top