![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Mystery (to me) Function
I picked this function up on the net. It is described as an accumulator generator, but I cannot figure out what it can be used for. Any help?
def make_acc(start=0):
"""accumulator generator """
curr = [start]
def acc(inc):
curr[0] += inc
return curr[0]
return acc
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
I'm guessing it's meant for debugging, to see how many times a function is being called. The only thing, is there's no argument to handle the function as a parameter. Once I added that in, and a default for "inc", I could make a demonstration.
def make_acc(func, start=0):
"""accumulator generator """
curr = [start]
def acc(inc=1):
curr[0] += inc
return curr[0]
return acc
@make_acc # use make_acc for debugging purposes
def foo():
return "bar"
print foo() #1
print foo() #2
print foo() #3
print foo() #4
# don't use "make_acc"
def foo():
return "bar"
print foo() #bar
print foo() #bar
print foo() #bar
print foo() #bar |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I understand how the accumulator function works, but how come the curr variable has to be a list? Why doesn't the following work?
def make_acc(start=0):
"""accumulator generator """
curr = start
def acc(inc):
curr += inc
return curr
return acc |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Wow Sane, I think you hit the hammer on the nail!
I think the list here behaves like a static variable.
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#5 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
To follow this up with an example: # Python's version of a static variable
def static_num(list1=[0]):
"""The function's default is a list with one element = zero.
Since the list is a mutable object it retains its address,
as the function's default is created at compile time.
Whatever you put into this list as element is retained too"""
list1[0] += 1
return list1[0]
print static_num() # 1
print static_num() # 2
print static_num() # 3
...
__________________
I looked it up on the Intergnats! Last edited by Dietrich; Aug 14th, 2006 at 3:38 PM. |
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
Remember that "curr += inc" is equivalent to "curr = curr + inc", so it's an assignment. Because it's the first such assignment in the local scope, it's also a declaration. Thus, curr is first defined, and assigned the value "curr + inc". Unfortunately, because curr is defined before the assignment is evaluated, the curr in "curr + inc" refers to the variable you've just defined. Since you haven't assigned this curr a value yet, an error arises. This problem can be circumvented by an explicit declaration keyword; let's call it "var". Then, the code would look like: def make_acc(start=0):
"""accumulator generator """
var curr = start
def acc(inc):
curr += inc
return curr
return accdef make_acc(start=0):
"""accumulator generator """
curr = [start]
def acc(inc):
curr[0] += inc
return curr[0]
return acc |
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
To which I add:
def make_acc(start=0):
def acc(inc):
acc.curr += inc
return acc.curr
acc.curr = start
return accAs to where you'd use an accumulator generator - you could use it with a language where functional programming is more pleasant than OO, and you want to store state for (eg) an accounting program - ie you need to store many totals. You could use an accumulator generator for each. Even with an OO program using one probably decreases the chances of bugs - "=" rather than "+=", for instance. You could probably also use it usefully for something domain-specific-ish in (eg) Ruby since you don't need brackets to call it. -T. *handwave* "These are not the droids you are looking for" *handwave* |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Aha! A rather elegant solution to the problem you have there, hydroxide.
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Small bit of background that may help explain part of the comment: I originally posted that code on devshed as what I considered to be a prettier way of writing an acc gen (and by extension any stateful closure). I misread Dietrich's reply as where would _I_ use an acc gen (answer "Never") - sorry, D. Glad someone liked it
.Python is probably either getting an "outer" declaration or else the "global" declaration will look in successively outer scopes. Not sure if this will be 2.6 or 3.0 tho. Cheers, -T. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Compiling Maverik 6.2 (from C) | megamind5005 | C | 16 | May 3rd, 2006 5:41 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| Php Postgresql Class | Pizentios | Show Off Your Open Source Projects | 15 | Jun 28th, 2005 9:55 AM |
| Jackpot game | zorin | Visual Basic | 3 | Jun 10th, 2005 1:19 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |