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

后端 未结 13 1775
旧时难觅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 11:02

    There isn't a way to do that within the class scope, not unless A was defined to be something else first (and then some_func(A) will do something entirely different from what you expect)

    Unless you're doing some sort of stack inspection to add bits to the class, it seems odd why you'd want to do that. Why not just:

    class A:
        # do something
        pass
    
    some_func(A)
    

    That is, run some_func on A after it's been made. Alternately, you could use a class decorator (syntax for it was added in 2.6) or metaclass if you wanted to modify class A somehow. Could you clarify your use case?

提交回复
热议问题