简体   繁体   English

执行高阶函数

[英]Executing higher-order functions

I'm learning the concepts of first class functions and closures in Python and I was curious to know:我正在学习 Python 中第一个 class 函数和闭包的概念,我很想知道:

Given a higher-order function:给定一个高阶 function:

def html_tag(tag):
    def wrap_text(text):
        print("<{0}>{1}</{0}>".format(tag, text))
    return wrap_text
  1. What's the difference between these two approaches of executing higher-order functions.这两种执行高阶函数的方法有什么区别。
  2. Are there any advantages of using one over the other.使用其中一种是否有任何优势。
  3. What is the best way to execute a higher-order function in a program.在程序中执行高阶 function 的最佳方法是什么。

1. 1.

print_h1 = html_tag('h1')
print_h1('Test Headline')
print_h1('Another Headline')

2. 2.

html_tag('h1')('Test Headline')
html_tag('h1')('Another Headline')

The only advantage I can think of is that by assigning the return value of html_tag to a variable in the first example, you prevent the code from having to execute again and return a new function each time.我能想到的唯一好处是,通过在第一个示例中将html_tag的返回值分配给变量,可以防止代码必须再次执行并每次返回一个新的 function。 Whereas in your second example, you are calling html_tag directly and it will produce a new function reference each time, which will result in a decrease in performance.而在第二个示例中,您直接调用html_tag并且每次都会产生一个新的 function 引用,这将导致性能下降。

Depends on your usage, but if you're passing in the same argument to html_tag , then I would go with your first example.取决于您的用法,但如果您将相同的参数传递给html_tag ,那么我会在您的第一个示例中使用 go 。 But if you need to use a different tag, then obviously you would have to re-call html_tag again with a different argument.但是如果您需要使用不同的标签,那么显然您必须使用不同的参数再次调用html_tag

In terms of the references to the function, this could be important, for example, if you were for some reason storing the function in a dict , so you wouldn't be able to lookup the function as a key unless you keep around a reference to the same function (as in your first example) In terms of the references to the function, this could be important, for example, if you were for some reason storing the function in a dict , so you wouldn't be able to lookup the function as a key unless you keep around a reference到相同的 function (如您的第一个示例)

In the example you give, there's no difference in the result.在您给出的示例中,结果没有区别。 The second way is less efficient, since you're creating an equivalent function multiple times, which is redundant.第二种方法效率较低,因为您要多次创建等效的 function,这是多余的。

It becomes more relevant when you have functions that keep state between runs.当您具有在运行之间保持 state 的功能时,它变得更加相关。

def counting_tag(tag):
    counter = 0
    def wrap_text(text):
        nonlocal counter
        counter += 1
        print("<{0}>{1}. {2}</{0}>".format(tag, counter, text))

Every time you call counting_tag() it will return a function with the counter reset back to 0 .每次调用 count_tag counting_tag()时,它都会返回 function ,计数器重置回0

print_h1 = counting_tag('h1')
print_h1('Test Headline')
print_h1('Another Headline')

This will print这将打印

1. Test Headline
2. Another Headline

But if you do it the second way:但是,如果您以第二种方式进行操作:

counting_tag('h1')('Test Headline')
counting_tag('h1')('Another Headline')

you'll get你会得到

1. Test Headline
1. Another Headline

The reason you'd have a higher order function would typically be so that you could easily generate named helper functions like print_h1 , as in your first example.您拥有更高阶 function 的原因通常是这样您就可以轻松生成像print_h1这样的命名辅助函数,就像在第一个示例中一样。 This has two benefits:这有两个好处:

  1. You avoid repeating the 'h1' string.您避免重复'h1'字符串。 Any time you have to copy and paste a string literal multiple places, it's an invitation for a bug to happen due to a typo!任何时候您必须在多个位置复制和粘贴字符串文字,这都会导致由于拼写错误而发生错误!
  2. Less typing.少打字。 (This is more like a secondary benefit of not repeating yourself.) (这更像是不重复自己的次要好处。)

If you were going to re-invoke html_tag each time, as in your second example, making a higher order function offers no benefit over simply doing:如果您要每次都重新调用html_tag ,就像在第二个示例中一样,那么制作更高阶的 function 与简单地执行以下操作相比没有任何好处:

def html_tag(tag, text):
    print("<{0}>{1}</{0}>".format(tag, text))


html_tag('h1', 'Test Headline')
html_tag('h1', 'Another Headline')

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

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