2011-05-18 159 views
39

代码:类型错误: 'STR' 对象不是可调用(Python)的

import urllib2 as u 
import os as o 
inn = 'dword.txt' 
w = open(inn) 
z = w.readline() 
b = w.readline() 
c = w.readline() 
x = w.readline() 
m = w.readline() 
def Dict(Let, Mod): 
    global str 
    inn = 'dword.txt' 
    den = 'definitions.txt' 

    print 'reading definitions...' 

    dell =open(den, 'w') 

    print 'getting source code...' 
    f = u.urlopen('http://dictionary.reference.com/browse/' + Let) 
    a = f.read(800) 

    print 'writing source code to file...' 
    f = open("dic1.txt", "w") 
    f.write(a) 
    f.close() 

    j = open('defs.txt', 'w') 

    print 'finding definition is source code' 
    for line in open("dic1.txt"): 
     if '<meta name="description" content=' in line: 
      j.write(line) 

    j.close() 

    te = open('defs.txt', 'r').read().split() 
    sto = open('remove.txt', 'r').read().split() 

    print 'skimming down the definition...' 
    mar = [] 
    for t in te: 
     if t.lower() in sto: 
      mar.append('') 
     else: 
      mar.append(t) 
    print mar 
    str = str(mar) 
    str = ''.join([ c for c in str if c not in (",", "'", '[', ']', '')]) 

    defin = open(den, Mod) 
    defin.write(str) 
    defin.write('     ') 
    defin.close() 

    print 'cleaning up...' 
    o.system('del dic1.txt') 
    o.system('del defs.txt') 
Dict(z, 'w') 
Dict(b, 'a') 
Dict(c, 'a') 
Dict(x, 'a') 
Dict(m, 'a') 
print 'all of the definitions are in definitions.txt' 

第一Dict(z, 'w')作品,然后在其周围所述第二时间想出了一个错误:

Traceback (most recent call last): 
    File "C:\Users\test.py", line 64, in <module> 
    Dict(b, 'a') 
    File "C:\Users\test.py", line 52, in Dict 
    str = str(mar) 
TypeError: 'str' object is not callable 

有人知道为什么吗?

@格雷格Hewgill:

我已经试过了,我得到的错误:

Traceback (most recent call last): 
File "C:\Users\test.py", line 63, in <module> 
    Dict(z, 'w') 
    File "C:\Users\test.py", line 53, in Dict 
    strr = ''.join([ c for c in str if c not in (",", "'", '[', ']', '')]) 
TypeError: 'type' object is not iterable 
+4

重命名您的'str'变量的所有实例,包括53行中该错误正在抱怨的那个实例。 – 2011-05-18 04:47:47

回答

79

这就是问题所在:

global str 

str = str(mar) 

您正在重新定义什么str()手段。 str是字符串类型的内置Python名称,您不想更改它。

为本地变量使用不同的名称,并删除global语句。

+6

并不意味着被使用,但是'str()'函数也可以用作__builtins __。str()'。当然,在99.9999%的案例中使用它是一个糟糕的主意,我想。 – n611x007 2013-09-26 20:23:35

+0

请记住,您需要重新启动python内核才能返回“str()”功能。 – yeliabsalohcin 2017-09-04 09:48:36

85

虽然不是在你的代码,当%字符的字符串格式化的尝试缺少另一个难以当场错误是:

"foo %s bar %s coffee"("blah","asdf") 

,但它应该是:

"foo %s bar %s coffee"%("blah","asdf") 

缺少%将导致相同的TypeError: 'str' object is not callable

+4

谢谢先生!简单的错误,但很大程度上影响了破坏脚本。 – ChrisR 2014-01-13 19:27:17

+0

我知道这真的很旧,但我刚刚遇到了这个问题,并且这个答案有帮助,谢谢@naxa – Bbit 2015-05-31 00:12:15

+0

这经过了一小时的故障排除后才救了我。谢谢! – Michael 2016-12-19 18:54:43

12

在我的情况下,我有一个类有一个方法和一个字符串属性的同名,我试图调用该方法,但获取字符串属性。

4

这种情况的另一种情况:与对象的__repr__函数混淆,其中format()调用不透明地失败。

在我们的案例中,我们在__repr__上使用了一个@property修饰器,并将该对象传递给format()。装饰器@property导致__repr__对象被转换为字符串,然后导致str对象不可调用错误。

+1

谢谢,这也是我的问题(我没有使用格式,但出于某种原因,在'__repr__'上有一个@property) – Justin 2016-05-17 18:01:24

+0

这个解决方案应该是真正高效的。 – 2018-02-09 20:09:58

0

我有同样的错误。在我的情况下,因为名为str的变量而不是。但是因为我用str参数命名了一个函数,并且这个变量也是一样的。

same_name = same_name(var_name:str)

我在循环中运行它。第一次运行正常。第二次我得到这个错误。将该变量重命名为与函数名称不同的名称解决了此问题。所以我认为这是因为Python曾经将一个函数名称关联到一个作用域中,第二次尝试将左边部分(same_name =)作为对该函数的调用进行关联,并检测到str参数不存在,那么它会抛出该错误。

-1

在我的情况下,我有一个类中有一个方法。该方法没有“self”作为第一个参数,当我调用该方法时抛出错误。一旦我将“self”添加到方法的参数列表中,就没有问题。

2

如果您有变量str并试图调用str()函数,则可以得到此错误。

0

一个问题我刚刚不小心被调用字符串

"Foo" ("Bar" if bar else "Baz") 

你可以只是把它们彼此相邻,像这样

"Foo" "Bar" 

但是因为在开括号的串联串第一个例子,它认为我试图呼叫"Foo"

0

它可能也是你试图以错误的方式索引:

a = 'apple' 
a(3) ===> 'str' object is not callable 

a[3] = l 
相关问题