Recursive method in class

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Fri Sep 27 08:07:47 EDT 2019


On 9/27/19 7:54 AM, ast wrote:
> Hello
> 
> Is it feasible to define a recursive method in a class ?
> (I don't need it, it's just a trial)
> 
> Here are failing codes:
> 
> 
> class Test:
>       def fib(self, n):
>           if n < 2: return n
>           return fib(self, n-2) + fib(self, n-1)

     return self.fib(n - 2) + self.fib(n - 1)

(1) This is a horribly ineffective way to compute Fibonacci
numbers, but that's not immediately relevant.

(2) Recursive instance methods only make sense when there's
some sort of state being maintained, which in this case there
is not.  But that's also not immediately relevant.



More information about the Python-list mailing list