Self Referencing Class Definition in python

后端 未结 6 1947
野的像风
野的像风 2020-12-01 11:00

is there any way to reference a class name from within the class declaration? an example follows:

class Plan(SiloBase):
    cost = DataField(int)
    start =         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 11:31

    i've got a metaclass that reads this information and does some setup

    Most frameworks that use metaclasses provide a way to resolve this. For instance, Django:

    subplan = ForeignKey('self')
    

    Google App Engine:

    subplan = SelfReferenceProperty()
    

    The problem with solutions like tacking an additional property on later or using __new__ is that most ORM metaclasses expect the class properties to exist at the time when the class is created.

提交回复
热议问题