我已经使用小数默认值定义了一个接收可选参数的函数:
def foo(x=0.1):
pass
Run Code Online (Sandbox Code Playgroud)
现在输入foo(IDLE shell时,弹出的工具提示可以帮助我完成调用(x=0<tuple>),而不是预期的(x=0.1).我之前从未遇到过这种情况,但我发现很难相信我没有使用任何带有小数默认值的函数/方法.
假设它是一个功能,而不是一个错误,如果有人可以解释它为什么会发生,我会很高兴.我在Windows 7上使用64位64位python.
编辑:
从评论来看,它似乎不是一个功能.我已经通过2rs2ts的建议检查了不同的函数定义,并且发现了我试图在工具提示中替换的小数点的每个外观.所以这个定义 -
def foo(x=[(1,0.1), 2, .3]):
pass
Run Code Online (Sandbox Code Playgroud)
生产工具尖端(x=[(1, 0<tuple>), 2, 0<tuple>]).
我应该关闭此问题并提交错误报告吗?
这是一个奇怪的答案,发现它感觉有点像白费力气......
我没有看到bugs.python.org上发布的问题,但经过一番探索后,我在 Python 2.6.6 中找到了 CallTips.py 文件,并且看到了可能有问题的代码行。通过向下滚动到get_arg_text()方法中的第 161 行,我看到
arg_text = "(%s)" % re.sub("\.\d+", "<tuple>", arg_text)
Run Code Online (Sandbox Code Playgroud)
这看起来就像您在问题中发布的内容,实际上,如果arg_text将浮点数转换为字符串,则该行返回字符串<tuple>:
arg_text = "(%s)" % re.sub("\.\d+", "<tuple>", "9.0") # returns (9<tuple>)
Run Code Online (Sandbox Code Playgroud)
但是,该问题必须已在svn.python.org/.../ Calltips.py(在 PEP 384 期间?)中得到修复,因为该版本没有上面的行。事实上,get_arg_text()被取代了get_argspec()。
所以答案似乎是在 PEP 384 期间修复了它。根据对您问题的评论,Python 3.3 的 IDLE 有此修复,但正如您所指出的,Python 2.7.5 没有。为了进行比较,我粘贴了下面的两种方法,以便有人能够准确解释 PEP 384 如何解决您所看到的问题。
希望能帮助到你。
旧版本的 Calltips.py 有get_arg_text(ob)(至少是您正在使用的 2.7.5 版本)。例如,在 Mac 上,它位于/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/idlelib/CallTips.py 。该函数定义如下:
def get_arg_text(ob):
"""Get a string describing the arguments for the given object"""
arg_text = ""
if ob is not None:
arg_offset = 0
if type(ob) in (types.ClassType, types.TypeType):
# Look for the highest __init__ in the class chain.
fob = _find_constructor(ob)
if fob is None:
fob = lambda: None
else:
arg_offset = 1
elif type(ob)==types.MethodType:
# bit of a hack for methods - turn it into a function
# but we drop the "self" param.
fob = ob.im_func
arg_offset = 1
else:
fob = ob
# Try to build one for Python defined functions
if type(fob) in [types.FunctionType, types.LambdaType]: # <- differs here!
argcount = fob.func_code.co_argcount
real_args = fob.func_code.co_varnames[arg_offset:argcount]
defaults = fob.func_defaults or []
defaults = list(map(lambda name: "=%s" % repr(name), defaults))
defaults = [""] * (len(real_args) - len(defaults)) + defaults
items = map(lambda arg, dflt: arg + dflt, real_args, defaults)
if fob.func_code.co_flags & 0x4:
items.append("...")
if fob.func_code.co_flags & 0x8:
items.append("***")
arg_text = ", ".join(items)
arg_text = "(%s)" % re.sub("\.\d+", "<tuple>", arg_text)
# See if we can use the docstring
doc = getattr(ob, "__doc__", "")
if doc:
doc = doc.lstrip()
pos = doc.find("\n")
if pos < 0 or pos > 70:
pos = 70
if arg_text:
arg_text += "\n"
arg_text += doc[:pos]
return arg_text
Run Code Online (Sandbox Code Playgroud)
位于 svn.python.org/.../Calltips.py 的相应函数似乎修复了一个错误。该方法已重命名为get_argspec:
def get_argspec(ob):
"""Get a string describing the arguments for the given object."""
argspec = ""
if ob is not None:
if isinstance(ob, type):
fob = _find_constructor(ob)
if fob is None:
fob = lambda: None
elif isinstance(ob, types.MethodType):
fob = ob.__func__
else:
fob = ob
if isinstance(fob, (types.FunctionType, types.LambdaType)):
argspec = inspect.formatargspec(*inspect.getfullargspec(fob))
pat = re.compile('self\,?\s*')
argspec = pat.sub("", argspec)
doc = getattr(ob, "__doc__", "")
if doc:
doc = doc.lstrip()
pos = doc.find("\n")
if pos < 0 or pos > 70:
pos = 70
if argspec:
argspec += "\n"
argspec += doc[:pos]
return argspec
Run Code Online (Sandbox Code Playgroud)