Python Recursion within Class

前端 未结 2 1589
闹比i
闹比i 2021-02-02 17:49

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          


        
2条回答
  •  不要未来只要你来
    2021-02-02 18:04

    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, ....)
    

提交回复
热议问题