如何将函数结果传递给元组?
How to pass a function result to a tuple?
我正在做一个 Python/Django/Wagtail 项目,有时我有一个像这样的 class:
class SuperClass(BaseClass):
body = StreamField([
('overview_speakers', OverviewSpeakers()),
])
def some_function():
return 'Hola'
OverviewSpeakers 是一个 class,它期待一个参数,我想尝试传递 some_function( )
我都试过了:
body = StreamField([
('overview_speakers', OverviewSpeakers(self.some_function())),
])
和
body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])
但它分别喊着 self 或 SuperClass 未定义。
我可以做什么来传递函数的结果?
您不能引用正在定义的class,但您可以在定义后为其添加属性。所以这应该有效:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
SuperClass.body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])
看看这个:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
body = StreamField([
('overview_speakers', OverviewSpeakers(some_function())),
])
我正在做一个 Python/Django/Wagtail 项目,有时我有一个像这样的 class:
class SuperClass(BaseClass):
body = StreamField([
('overview_speakers', OverviewSpeakers()),
])
def some_function():
return 'Hola'
OverviewSpeakers 是一个 class,它期待一个参数,我想尝试传递 some_function( )
我都试过了:
body = StreamField([
('overview_speakers', OverviewSpeakers(self.some_function())),
])
和
body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])
但它分别喊着 self 或 SuperClass 未定义。
我可以做什么来传递函数的结果?
您不能引用正在定义的class,但您可以在定义后为其添加属性。所以这应该有效:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
SuperClass.body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])
看看这个:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
body = StreamField([
('overview_speakers', OverviewSpeakers(some_function())),
])