Python TypeError:“long”对象不可调用
我现在脑子里有很多想法,但我不知道我的代码出了什么问题。 以下是相关线路:Python TypeError:“long”对象不可调用,python,Python,我现在脑子里有很多想法,但我不知道我的代码出了什么问题。 以下是相关线路: try: outport = record_dict[id][hash_ % len(record_dict[id])] except: fp.write("Problem-"+str(type(record_dict[id]))+"\n") fp.write("Problem-"+str(record_dict[id])+"\n") fp.write("Problem-"+str(len(r
try:
outport = record_dict[id][hash_ % len(record_dict[id])]
except:
fp.write("Problem-"+str(type(record_dict[id]))+"\n")
fp.write("Problem-"+str(record_dict[id])+"\n")
fp.write("Problem-"+str(len(record_dict[id]))+"\n")
以下是我得到的错误:
File "xxxx.py", line 459, in yyyyy
fp.write("Problem-"+str(len(record_dict[id]))+"\n")
TypeError: 'long' object is not callable
fp指向的内部文件:
Problem-<type 'list'>
Problem-[5, 6, 7, 8]
我的代码有什么问题?如何调试它?您是否在任何地方创建了名为str或len的变量?如果是这样,那就是你的问题。最有可能是len,因为str在早期使用时没有任何问题 Python内置项不是保留的——这意味着您可以自由地将它们重新分配给您想要的任何对象。看起来您将len赋给了一个长整数,这很有意义,因为len在其他语言中是一个非常合理的变量名
您应该注意的是,不要通过创建相同名称的变量来隐藏内置函数。它会产生难以调试的问题。作为旁注:bare exception子句是最糟糕的异常处理方案-您不知道会发生什么异常,并且丢失了存储在异常回溯中的所有有用调试信息。FWIW,sys.exit实际上是通过引发python运行时捕获的SysExit异常来实现的 如果您处于循环中,希望记录有关当前迭代的异常的信息并继续下一项,请确保您没有捕获SysExit并学习使用日志模块:
import logging
# this will require some minimal conf somewhere, cf the fine manual
logger = logging.getLogger("my-logger-name")
def myfunction(somesequence):
for item in somesequence:
try:
result = process(item)
except Exception, e:
# in 'recent' python version this will not catch SysExit
# please refer to the doc for your python version
# if it's a slightly outdated version, uncomment the
# following lines:
# if isinstance(e, SysExit):
# raise
logger.exception("got %s on item %s", e, item)
continue
else:
# ok for this item
do_something_with(result)
我在另一个环境中遇到了这个问题:
slope = (nsize*iopsum - (osum)(isum)) / (nsize*oopsum - (osum)*2)
当然,我得到它的原因是osumisum,它将osum解释为一个数字,并试图在上面调用不同的数字。所以在解释之后,它看起来像这样:15133541,这没有任何意义
我通过在正确的位置osum*isum添加*来修复它。没问题。请注意变量名:我实际上是在使用其他人的代码库。有人会受伤的。