Python Recursion within Class
python
Solution
Each method of a class has to have `self` as a first parameter, i.e. do this:
def recur(self, num):
and it should work now.
Basically what happens behind the scene is when you do
instance.method(arg1, arg2, arg3, ...)
Python does
Class.method(instance, arg1, arg2, arg3, ....)
Problem
I just learn python today, and so am thinking about writing a code about recursion, naively. So how can we achieve the following in python? ``` class mine: def inclass(self): self = mine(); def recur(num): print(num, end="") if num > 1: print(" * ",end="") return num * self.recur(num-1) print(" =") return 1 def main(): a = mine() print(mine.recur(10)) main() ``` I tried to define self, but could not think of a way to do so. Any suggestions? Thank you very much. Yes the following work, thanks. ``` class mine: def recur(self, num): print(num, end="") if num > 1: print(" * ",end="") return num * self.recur(self, num-1) print(" =") return 1 def main(): a = mine() print(mine.recur(mine, 10)) main() ```