python中嵌套函数(函数函数)的用途

时间:2013-10-09 04:19:26

标签: python function methods openerp

以下代码可以在OpenERP 6.1的产品模块的product.py中找到

    _columns = {
        'qty_available': fields.function(_product_qty_available, type='float', string='Quantity On Hand'),
        'virtual_available': fields.function(_product_virtual_available, type='float', string='Quantity Available'),
...


    def _get_product_available_func(states, what):
        def _product_available(self, cr, uid, ids, name, arg, context=None):
            return {}.fromkeys(ids, 0.0)
        return _product_available

   _product_qty_available = _get_product_available_func(('done',), ('in', 'out'))
   _product_virtual_available = _get_product_available_func(('confirmed','waiting','assigned','done'), ('in', 'out'))

有人可以向我解释一般在python中定义方法内部的方法的目的吗?

5 个答案:

答案 0 :(得分:3)

我个人使用嵌套函数,如果它们是一种辅助函数,只在local方法中本地使用。当然,您可以将方法移动到模块级别并使其更“可见”。对我来说,在嵌套函数对模块级别指定的代码不感兴趣的某些情况下,它们提供了更好地封装代码的机会。

答案 1 :(得分:1)

使用内部方法的一个潜在好处是它允许您使用外部方法局部变量而不将它们作为参数传递。

def helper(feature, resultBuffer):
  resultBuffer.print(feature)
  resultBuffer.printLine()
  resultBuffer.flush()

def save(item, resultBuffer):

  helper(item.description, resultBuffer)
  helper(item.size, resultBuffer)
  helper(item.type, resultBuffer)

可以写成如下,可以说是更好的

def save(item, resultBuffer):

  def helper(feature):
    resultBuffer.print(feature)
    resultBuffer.printLine()
    resultBuffer.flush()

  helper(item.description)
  helper(item.size)
  helper(item.type)

当返回内部函数(将其移动到外部作用域)或将其传递到另一个函数时,它通常很有用。

嵌套函数可以访问定义它的环境。

具体关于product.py代码,它是关于开发人员选择管理代码以保持简短的可读功能和定义更多可用作API的功能。

谢谢

答案 2 :(得分:0)

在给出的例子中,我无法告诉你。通常,您在函数中声明一个函数有两个原因:使用原始函数参数作为上下文返回一个新函数,或者快速封装仅在原始函数内重复的函数。

示例1:

def partial_add(value):
    def value_plus(y):
        return value + y
    return value_plus

f = partial_add(5)
f(6)
> 11
f(f(5))
> 15

示例2:

def print_weird(p1, p2, p3, item, constant, k):
    def related(v, u):
        return v.score(u, item) < constant*(k+1)/k
    if related(p1, p2) and related(p2, p3) and related(p3, p1):
        print "weird"

答案 3 :(得分:0)

我希望在其他答案中解释嵌套功能及其用途的使用。所以我不去。在openerp中,如果您只检查产品模块中的product.py文件,那么使用嵌套功能会有点混乱。它实际上用于库存模块。如果您查看库存中的product.py文件,您将能够找到它的使用。

答案 4 :(得分:-2)

使用此示例解决您的问题 happyBirthdayEmily

def happyBirthdayEmily(): #program does nothing as written
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Emily.")
    print("Happy Birthday to you!")

birthday3.py

'''Function definition and invocation.'''

def happyBirthdayEmily():
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Emily.")
    print("Happy Birthday to you!")

happyBirthdayEmily()
happyBirthdayEmily()

birthday4.py我们在其中添加了一个函数happyBirthdayAndre,并将它们都称为

'''Function definitions and invocation.'''

def happyBirthdayEmily():
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Emily.")
    print("Happy Birthday to you!")

def happyBirthdayAndre():
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Andre.")
    print("Happy Birthday to you!")

happyBirthdayEmily()
happyBirthdayAndre()

如果我们希望程序在运行时自动执行任何操作,我们需要在定义之外的一行!最后一行是唯一一个直接执行的行,它调用main中的代码,而main又调用其他两个函数中的代码。