TypeError:xeger()需要2个参数(3给出)
问题描述:
我是新来的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)
,但我仍然收到相同的错误。TypeError:xeger()需要2个参数(3给出)
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)
答
在他们的源代码来看,有两件事情你可以做:
不要发送
limit
说法 - 它会默认为10.初始化它稍有不同。
像这样的事情
from xeger import Xeger
_x = Xeger(limit=limit)
out = _x.xeger(pattern)