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.

Closures

P: n/a
> Would you trade the "mutable integer" and "count(n=[0])" kludges for a
solution like &count ? Does anyone agree with me that a "closure
variable prefix" is more explicit and readable?


import sys

class Closure:
def __init__(self):
self.__dict__ = sys._getframe().f_back.f_locals

def getFunc():
counter = 0
c = Closure()
def count():
c.counter += 1
print c.counter
return count

c = getFunc()
c()
c()
c()

--
Gustavo Niemeyer
http://niemeyer.net

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Gustavo Niemeyer <ni******@conectiva.com> wrote in message news:<ma**************************************@pyt hon.org>...
import sys

class Closure:
def __init__(self):
self.__dict__ = sys._getframe().f_back.f_locals

def getFunc():
counter = 0
c = Closure()
def count():
c.counter += 1
print c.counter
return count

c = getFunc()
c()
c()
c()

This is a nice hack! Why not to make it available as a Cookbook recipe?

Michele Simionato
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.