python: how to refer to the class from within it ( like the recursive function)

后端 未结 13 1765
旧时难觅i
旧时难觅i 2020-12-10 10:10

For a recursive function we can do:

def f(i):
  if i<0: return
  print i
  f(i-1)

f(10)

However is there a way to do the following thin

13条回答
  •  自闭症患者
    2020-12-10 10:46

    In Python you cannot reference the class in the class body, although in languages like Ruby you can do it.

    In Python instead you can use a class decorator but that will be called once the class has initialized. Another way could be to use metaclass but it depends on what you are trying to achieve.

提交回复
热议问题