Re: [Python-Dev] recursive closures - reference cycle

2009-12-09 Thread Martin v. Löwis
> Yes, and a number of different workarounds. That's not really the > issue. The issue is that what looks like a perfectly safe idiom > (calling a function recursively) happens to create a reference cycle > if that function is a closure. This is a non-obvious "gotcha" that I > must now educate my

Re: [Python-Dev] recursive closures - reference cycle

2009-12-09 Thread Greg Ewing
Kristján Valur Jónsson wrote: Yes, and a number of different workarounds. That's not really the issue. The issue is that what looks like a perfectly safe idiom (calling a function recursively) happens to create a reference cycle if that function is a closure. This is a non-obvious "gotcha" t

Re: [Python-Dev] recursive closures - reference cycle

2009-12-09 Thread Kristján Valur Jónsson
> -Original Message- > From: python-dev-bounces+kristjan=ccpgames@python.org > [mailto:python-dev-bounces+kristjan=ccpgames@python.org] On Behalf > Of Greg Ewing > Sent: 8. desember 2009 22:09 > To: python-dev@python.org > Subject: Re: [Python-Dev] recursive

Re: [Python-Dev] recursive closures - reference cycle

2009-12-08 Thread Greg Ewing
You could use a class: class factorial(): def fac(self, n): if n == 0: return 1 else: return n * self.fac(n - 1) def __call__(self, n): return self.fac(n) factorial = factorial() print factorial(5) -- Greg _

Re: [Python-Dev] recursive closures - reference cycle

2009-12-08 Thread Kristján Valur Jónsson
> -Original Message- > From: python-dev-bounces+kristjan=ccpgames@python.org > [mailto:python-dev-bounces+kristjan=ccpgames@python.org] On Behalf > Of Antoine Pitrou > Sent: 8. desember 2009 14:55 > To: python-dev@python.org > Subject: Re: [Python-De

Re: [Python-Dev] recursive closures - reference cycle

2009-12-08 Thread Antoine Pitrou
Kristján Valur Jónsson ccpgames.com> writes: > > The problem with this is that once you have called > factorial() once, you end up with a recursive cycle. You don't need a closure to exhibit a reference cycle. A global function is enough: >>> def helper(n): ... if n: ... return n*helper(n-1