How to refer to the class from within it (like a recursive function)

class, python

Solution

If I understand your question correctly, you should be able to reference class A within class A by putting the type annotation in quotes. This is called forward reference.

class A:
  # do something
  def some_func(self, a: 'A')
  # ...

See ref below

- https://github.com/python/mypy/issues/3661

- https://www.youtube.com/watch?v=AJsrxBkV3kc

Problem

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 thing? ``` class A: # do something some_func(A) # ... ```

Original source

Related problems