how do you use a closure in a class
Paul McGuire
ptmcg at austin.rr.com
Tue Mar 29 23:48:18 EST 2005
Well, despite my parenthetical disclaimer, my attempted point was that
the OP wanted to avoid replicating several functions that were mostly
the same. I think Python's idiom of using a function to create and
return callables is a comparable feature to using anonymous closures.
Unfortunately, I guess the verbosity of the tallyFn calls was too much
of a distraction. The cut-and-paste alternative (which the OP was
trying to avoid) would be:
def tallyConsonant():
....tally["consonant] += 1
def tallyVowel():
....tally["vowel"] += 1
def tallyNotSure():
....tally["notsure"] += 1
def tallyOther():
....tally["other"] += 1
I was hoping to offer an example of the Pythonic
function-returns-a-callable idiom, instead of having this discussion
spiral down into another "what we really need are brace-enclosed
anonymous enclosures" futility.
-- Paul
More information about the Python-list
mailing list