[英]Explain lambda argparse.HelpFormatter(prog, width)
This code works properly to increase the width of the help text, but it's unclear. 此代码可以正常工作以增加帮助文本的宽度,但目前还不清楚。 What is the lambda function doing?
lambda函数在做什么?
EDIT: To clarify, the question is not Why are lambda functions useful in general , but instead, how is the argument parser init code using the lambda function ? 编辑:澄清,问题不是为什么lambda函数一般有用 ,而是参数解析器初始化代码如何使用lambda函数 ?
import argparse
import sys
formatter = lambda prog: argparse.HelpFormatter(prog, width=100)
dummy_text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum."""
parser = argparse.ArgumentParser(description=dummy_text, formatter_class=formatter)
parser.add_argument("-e", dest="destE", help=dummy_text)
parser.add_argument("-w", dest="destW", help=dummy_text)
args = parser.parse_args(sys.argv)
This is the __init__
for the default HelpFormatter class is: 这是默认的HelpFormatter类的
__init__
:
def __init__(self,
prog,
indent_increment=2,
max_help_position=24,
width=None):
The ArgumentParser
class uses this function to fetch a Formatter instance. ArgumentParser
类使用此函数来获取Formatter实例。 This instance is used by format_help
to create the help message. format_help
使用此实例来创建帮助消息。
def _get_formatter(self):
return self.formatter_class(prog=self.prog)
where self.formatter_class
is the parameter you set. 其中
self.formatter_class
是您设置的参数。 So the default invocation only sets the prog
parameter. 因此,默认调用仅设置
prog
参数。
formatter = lambda prog: argparse.HelpFormatter(prog, width=100)
calls the HelpFormatter
with the addition of the width
parameter. 通过添加
width
参数调用HelpFormatter
。
Here's an equivalent use of lambda
with a simpler function: 这是
lambda
的等效用法,具有更简单的功能:
In [176]: def foo(x,y):
...: return x,y
...:
In [177]: bar = lambda y: foo('x_str',y)
In [178]: bar('y_str')
Out[178]: ('x_str', 'y_str')
There are other ways of doing the same thing, such as 还有其他方法可以做同样的事情,比如
def formatter(prog):
return argparse.HelpFormatter(prog, width=100)
or a HelpFormatter
subclass. 或者是
HelpFormatter
子类。
The lambda here is simply "fixing" one of the parameters of the argparse.HelpFormatter
constructor. 这里的lambda只是“修复”
argparse.HelpFormatter
构造函数的一个参数。 The formatter
argument to argparse.ArgumentParser
takes a class that accepts one argument in its constructor. argparse.ArgumentParser
的formatter
参数接受一个在其构造函数中接受一个参数的类。 We would like to pass additional named arguments to the call we are using there... namely width=100
. 我们想将其他命名参数传递给我们在那里使用的调用...即
width=100
。 The way to do that is to create a second constructor that takes the same positional arguments as argparse.HelpFormatter
, but "fixes" width=100
in the call. 这样做的方法是创建第二个构造函数,该构造函数采用与
argparse.HelpFormatter
相同的位置参数,但在调用中“修复” width=100
。
This is a common paradigm when passing functions as arguments. 将函数作为参数传递时,这是一种常见的范例。 Another common example is when a function takes an argument that requires a function of one variable.
另一个常见的例子是当一个函数接受一个需要一个变量函数的参数时。 We have a function of two variables that we'd like to pass in, with one of the variables "fixed", so we use
new_func = lambda x: old_func(x, 5)
. 我们有两个变量的函数,我们想要传入,其中一个变量是“fixed”,所以我们使用
new_func = lambda x: old_func(x, 5)
。 Checkout functools.partial
结帐
functools.partial
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.