且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python中的自引用类定义

更新时间:2023-02-03 07:40:27

我有一个元类来读取这些信息并进行一些设置

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

大多数使用元类的框架都提供了解决此问题的方法.例如,Django:

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

subplan = ForeignKey('self')

Google 应用引擎:

subplan = SelfReferenceProperty()

诸如在稍后添加附加属性或使用 __new__ 等解决方案的问题在于,大多数 ORM 元类都希望类属性在创建类时存在.

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.