我正在研究Python / Django / Wagtail项目,在某些时候我有一个这样的类:
class SuperClass(BaseClass):
body = StreamField([
('overview_speakers', OverviewSpeakers()),
])
def some_function():
return 'Hola'
OverviewSpeakers 是一个期待参数的类,我想尝试传入 some_function()
的结果我试过了两个:
body = StreamField([
('overview_speakers', OverviewSpeakers(self.some_function())),
])
和
body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])
但是它分别大喊 self 或 SuperClass 未定义。
我可以做些什么来传递函数的结果?
答案 0 :(得分:1)
检查出来:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
body = StreamField([
('overview_speakers', OverviewSpeakers(some_function())),
])
答案 1 :(得分:0)
您不能引用正在定义的类,但可以在定义后向其添加属性。所以这应该有效:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
SuperClass.body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])