> 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
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
> -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
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
_
> -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
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