python类属性继承 [英] python class attribute inheritance
本文介绍了python类属性继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过编写以下代码来节省输入时间,但似乎无法做到这一点:
I am trying to save myself a bit of typing by writing the following code, but it seems I can't do this:
class lgrAdminObject(admin.ModelAdmin):
fields = ["title","owner"]
list_display = ["title","origin","approved", "sendToFrames"]
class Photos(lgrAdminObject):
fields.extend(["albums"])
为什么不起作用?而且由于它们不起作用,所以我不能做超级把戏
why doesn't that work? Also since they're not functions, I can't do the super trick
fields = super(Photos, self).fields
fields.extend(["albums"])
推荐答案
继承在类的主体执行后 应用。在类主体中,您可以使用 lgrAdminObject.fields
-您确定要更改超类的属性,而不是先复制它吗?似乎很奇怪...我将从副本开始:
Inheritance applies after the class's body executes. In the class body, you can use lgrAdminObject.fields
-- you sure you want to alter the superclass's attribute rather than making a copy of it first, though? Seems peculiar... I'd start with a copy:
class Photos(lgrAdminObject):
fields = list(lgrAdminObject.fields)
,然后继续进行更改。
这篇关于python类属性继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文