Python-直接嵌套函数调用

Python-直接嵌套函数调用,python,nested-function,Python,Nested Function,我想直接调用嵌套函数,如下所示: 模板('/path/to/file')。预期('key')。进入('second'key') Template('/path/to/file')。expect('key')。to__be('value') 我试过这个: class Template(object): def __init__(self, path): self.content = json.load(open(path, 'r')) def expect(self, a):

我想直接调用嵌套函数,如下所示:

模板('/path/to/file')。预期('key')。进入('second'key')

Template('/path/to/file')。expect('key')。to__be('value')

我试过这个:

class Template(object):
  def __init__(self, path):
    self.content = json.load(open(path, 'r'))

  def expect(self, a):
    def to_be_in(b):
      b = self.content[b]
      return a in b

    def to_be(b):
      a = self.content[b]
      return a == b
但我得到了以下错误:

Template('~/template.json').expect('template_name').to_be_in('domains')

AttributeError: 'NoneType' object has no attribute 'to_be_in'

如何在Python中实现这一点?

您必须返回一个对象,该对象提供一个函数中的
to\u be\u成员(仅示例):


该嵌套函数不存在于父函数之外!这正是我在记事本上写的。谢谢
class Template_Expect(object):
    def __init__(self, template, a):
        self.template = template
        self.a = a

    def to_be_in(self, b):
        b = self.template.content[b]
        return self.a in b

    def to_be(self, b):
        a = self.template.content[b]
        return a == b

class Template(object):
    def __init__(self, path):
        self.content = json.load(open(path, 'r'))

    def expect(self, a):
        return Template_Expect(self, a)