Does Python have class prototypes (or forward declarations)?

前端 未结 6 1759
不思量自难忘°
不思量自难忘° 2020-11-28 10:06

I have a series of Python classes in a file. Some classes reference others.

My code is something like this:

class A():
    pass

class B():
    c = C         


        
6条回答
  •  时光说笑
    2020-11-28 10:08

    All correct answers about class vs instance attributes. However, the reason you have an error is just the order of defining your classes. Of course class C has not yet been defined (as class-level code is executed immediately on import):

    class A():
        pass
    
    class C():
        pass
    
    class B():
        c = C()
    

    Will work.

提交回复
热议问题