我是python的新手,我正在尝试运行命令行,但它失败并显示错误TypeError: xeger() takes exactly 2 arguments (3 given)
。
我无法理解原因,因为我只向xeger.xeger发送了两个参数:out = xeger.xeger(pattern, limit)
。我了解self
已隐藏但会自动添加到参数列表中,因此我已更改为xeger.xeger(self, pattern, limit)
并将函数签名更改为def generate(self, limit, pattern)
但我仍然遇到相同的错误。
import xeger
import click
@click.command()
@click.option('--limit', default=10, help='Number of chars to return')
@click.option('--pattern', prompt='pattern to match', help='regexp to match')
def generate(limit, pattern):
"""Simple program that receives a pattern and returns a matching string."""
out = xeger.xeger(pattern, limit)
click.echo('%s' % out)
if __name__ == '__main__':
generate()
运行并出错:
python rand.py --limit 230
pattern to match: asda
Traceback (most recent call last):
File "rand.py", line 13, in <module>
generate()
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "rand.py", line 9, in generate
out = xeger.xeger(pattern, limit)
TypeError: xeger() takes exactly 2 arguments (3 given)
答案 0 :(得分:0)
查看他们的源代码,您可以做两件事:
不要发送limit
参数 - 默认为10。
稍微不同地初始化它。
像这样的东西
from xeger import Xeger
_x = Xeger(limit=limit)
out = _x.xeger(pattern)