Python - 直接嵌套函数调用

时间:2016-02-07 04:49:29

标签: python nested-function

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

Template('/path/to/file').expect('key').to_be_in('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中实现这一目标?

1 个答案:

答案 0 :(得分:3)

您必须返回一个提供function maxtwo_right(a){ var b1 = -Infinity; var b2 = -Infinity; for (var i=0; i < a.length; i++) { // If the current entry is bigger than variable b1 // keep the old value in the variable b2 and set b1 to the // value of the current entry if (a[i] > b1) { b2 = b1; b1 = a[i]; } // if the current entry equals b1 set the variable b2 to // the value of the current entry else if(a[i] === b1){ b2 = a[i]; } } // return the sum of the two variables as requested return b1 + b2; } 成员函数的对象(仅限示例):

to_be_in