使用类成员在python中格式化字符串

who*_*iac 5 python

考虑以下代码:

class Foo:
  def geta(self):
    self.a = 'lie'
    return 'this is {self.a}'.format(?)
Run Code Online (Sandbox Code Playgroud)

我应该写什么而不是问号,以便字符串格式正确?

glg*_*lgl 11

你可能正在寻找的是

'this is {0.a}'.format(self)
'this is {.a}'.format(self)
'this is {o.a}'.format(o=self)
'this is {self.a}'.format(self=self)
Run Code Online (Sandbox Code Playgroud)

但请注意,您至少缺少班级中的某个方法.

直接在类范围内没有这样的东西self.