简体   繁体   English

Python - 内部函数,闭包和工厂函数 - 如何分解?

[英]Python - Inner Functions, Closures, and Factory Functions - how to factor out?

I'm hoping a Python expert out there can offer some assistance on the confusion I'm experiencing currently with Inner Functions, Closures, and Factory Functions. 我希望那里的Python专家可以为我目前在内部功能,闭包和工厂功能方面遇到的困惑提供一些帮助。 Upon looking for an implemented example of a General Hough Transform I found this: 在寻找General Hough Transform的实现示例时,我发现了这个:

https://github.com/vmonaco/general-hough/blob/master/src/GeneralHough.py https://github.com/vmonaco/general-hough/blob/master/src/GeneralHough.py

I'd like to translate this into C++ and it seems the first step is to factor out the inner function in general_hough_closure(): 我想把它翻译成C ++,似乎第一步是将general_hough_closure()中的内部函数分解出来:

def general_hough_closure(reference_image):
    '''
    Generator function to create a closure with the reference image and origin
    at the center of the reference image

    Returns a function f, which takes a query image and returns the accumulator
    '''
    referencePoint = (reference_image.shape[0]/2, reference_image.shape[1]/2)
    r_table = build_r_table(reference_image, referencePoint)

    def f(query_image):
        return accumulate_gradients(r_table, query_image)

    return f

I seem to be stuck on how this function works. 我似乎被困在这个功能如何工作。 "f" does not seem to be called anywhere, and I'm not sure how the function knows what "query_image" is? 似乎没有在任何地方调用“f”,我不确定函数如何知道“query_image”是什么? I'v tried various Googling to find tips on Inner Functions, Closures, and Factory Functions, for example this and some similar pages, but all the examples I can find are more simplified and therefore not much help. 我尝试了各种谷歌搜索,找到关于内部功能,闭包和工厂功能的提示,例如这个和一些类似的页面,但我能找到的所有例子都更加简化,因此没什么帮助。 Can anybody offer some direction? 任何人都可以提供一些方向吗?

The code is just returning the function f as a whole thing. 代码只是返回函数f作为一个整体。 There's no need to "know what the argument is" -- f will know it at the time it is called. 没有必要“知道论证是什么” - f在被调用时会知道它。 The classic example is this: 经典的例子是这样的:

>>> def f(x):
...     def g(y):
...         return x + y
...     return g
... 
>>> f
<function f at 0x7f8500603ae8>
>>> f(1)
<function f.<locals>.g at 0x7f8500603a60>
>>> s = f(1)
>>> s(2)
3

Here, as in your function, g closes over another value ( x or r_table , respectively), while still expecting its actual argument. 这里,就像你的函数一样, g 关闭另一个值(分别是xr_table ),同时仍然期望它的实际参数。

Since there is a closed-over value, you cannot directly factor out f . 由于存在封闭值,因此无法直接分解f One traditional approach is to return an object containing the value, which has some kind of call method representing the function; 一种传统的方法是返回一个包含该值的对象,该对象具有某种表示该函数的调用方法; the easier way in C++ nowadays is to use a lambda function: 现在C ++中更简单的方法是使用lambda函数:

int f(int x) {
  auto g = [x](int y) {
    return x + y
  };
  return g;
}

In C++ you have the "advantage" that it will yell at you if don't specify which values you are closing over (that's the [x] here). 在C ++中,如果不指定要关闭的值(这是[x]这里),你会有“优势”它会对你大喊大叫。 But internall, it does pretty much the same thing (constructing an anonymous class with an x -member). 但是在内部,它几乎完全相同(构建一个带有x -member的匿名类)。

C++ before C++11 does not have function as type. C ++ 11之前的C ++没有类型的函数。

You can use the following class to emulate the semantics (pseudo code): 您可以使用以下类来模拟语义(伪代码):

class GeneralHoughClosure {
  public:
    GeneralHoughClosure(reference_image) {
      referencePoint = (reference_image.shape[0]/2, reference_image.shape[1]/2)
      r_table = build_r_table(reference_image, referencePoint)
    }
    void Run(queryImage) {
      return accumulate_gradients(r_table, query_image)
    }
    void operator()(queryImage) {
      return accumulate_gradients(r_table, query_image)
    }        
  }

Then, you can use it as follows: 然后,您可以按如下方式使用它:

gg = new GeneralHoughClosure(reference_image)
gg.Run(queryImage1)
gg(queryImage2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM