473,761 Members | 10,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Apr 3 '08 #1
2 1339
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.
Apr 4 '08 #2
En Thu, 03 Apr 2008 21:21:11 -0300, Dan Bishop <da*****@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]
http://www.python.org/doc/faq/genera...etween-objects

--
Gabriel Genellina

Apr 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4216
by: Chris Reedy | last post by:
I would like to memoize (I think I've got that right) a collection of functions for a project I'm working. <Aside for those not familiar with memoizing functions:> Given: def foo(a,b,c): return <result of large ugly computation>
3
4019
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from Pickle.memoize(): assert id(obj) not in self.memo I'm shrinking the offending data structure down to find the problem
13
2357
by: km | last post by:
Hi all, was going thru the new features introduced into python2.4 version. i was stuck with 'decorators' - can someone explain me the need of such a thing called decorators ? tia KM
1
1457
by: Mosas | last post by:
Dear All, Php has exec function to run system commands and this function return last line of system command result. But in Python I can see only os.system() function to run system command but it doesn't return any result like php exec function. I want to know about php equivalent exec function in Python. So If anyone know regarding this Kindly Mail me. with regards
0
1595
by: Paul Hsieh | last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote: > There was a recent thread in this group which talked about the > shortcomings of fgets(). I decided to try my hand at writing a > replacement for fgets() using fgetc() and realloc() to read a line of > arbitrary length. I know that the better programmers in this group could > write a more robust function, but here is my shot at it anyway. > I would appreciate people's comments on my...
0
954
by: sonu | last post by:
Hi all, I have a problem regarding use of manualreset events in .net. I have a developed a sample appliation in which entries from the listview which I say is a queue for my processes.I want to schedule these processes in my queue. I also want to stop the processing by selcting the pause so that the scheduling stops and again if i say add schedule the schedule should start again.I have used manual reset events to control the...
77
4030
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that goto sometimes makes program some more cleaner and easy to understand and also quite useful (in error handling cases). So why goto is outlawed from civilized c programmers community. is there any technical inefficiency in that.
11
2519
by: thattommyhallll | last post by:
im plugging away at the problems at http://www.mathschallenge.net/index.php?section=project im trying to use them as a motivator to get into advanced topics in python. one thing that Structure And Interpretation Of Computer Programs teaches is that memoisation is good. all of the memoize decorators at the python cookbook seem to make my code slower. is using a decorator a lazy and inefficient way of doing memoization? can anyone point...
1
2715
by: bg_ie | last post by:
I'm designing a database with 3 tables called Function, Test and Scene. A Function has multiple Tests, but a Test has only one Function. A many to many relationship exists between Test and Scene therefore I need a junction table between these two tables - giving 4 tables in total. The Test table would store a foreign key, the primary key of the Function table. There is a problem with design though and that is that Functions and
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.