TypeError:'str'对象不可调用(Python)

P's*_*sao 59 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'
Run Code Online (Sandbox Code Playgroud)

第一次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
Run Code Online (Sandbox Code Playgroud)

有人知道为什么吗?

@Greg 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
Run Code Online (Sandbox Code Playgroud)

n61*_*007 106

虽然不在您的代码中,但另一个难以发现的错误是%在尝试字符串格式化时缺少该字符:

"foo %s bar %s coffee"("blah","asdf")
Run Code Online (Sandbox Code Playgroud)

但它应该是:

"foo %s bar %s coffee"%("blah","asdf")
Run Code Online (Sandbox Code Playgroud)

失踪%将导致相同的结果TypeError: 'str' object is not callable.

  • 谢谢你,先生!简单的错误但严重影响了破坏脚本. (4认同)

Gre*_*ill 105

这就是问题:

global str

str = str(mar)
Run Code Online (Sandbox Code Playgroud)

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

为局部变量使用不同的名称,并删除该global语句.

  • 不打算使用,但是,`str()`函数也可用作`__builtins __.str()`.当然,我认为在99.9999%的情况下使用它是个坏主意. (7认同)
  • 请记住,您需要重新启动 python 内核才能返回 `str()` 功能。 (3认同)
  • @yeliabsalohcin,如果您不小心覆盖了它,则可以使用del str,并且将恢复内置功能。 (2认同)

Tho*_*ner 22

在我的情况下,我有一个类有一个方法和一个同名的字符串属性,我试图调用该方法,但得到了字符串属性.


Dmi*_*try 10

如果您有变量str并尝试调用str()函数,则可能会出现此错误.


chi*_*tin 9

每当发生这种情况时,只需发出以下命令(它也在上面发布)

>>> del str
Run Code Online (Sandbox Code Playgroud)

那应该解决它。


Luc*_*ano 7

另一种情况:使用非透明调用失败__repr__的对象的功能进行混乱format().

在我们的例子中,我们使用了一个@property装饰器__repr__并将该对象传递给了一个format().该@property装饰使__repr__被打开成一个字符串,其然后导致对象str对象是不可调用错误.


小智 6

重要的是要注意(如果您是Google来的,请注意)“ TypeError:'str'对象不可调用 ”意味着仅尝试将先前声明为String-type的变量用作函数(例如,通过最后添加括号。)

如果您使用任何其他内置方法作为变量名称,则也可以获得完全相同的错误消息。


小智 5

我有同样的错误。就我而言,不是因为名为 的变量str。但是因为我命名的函数带有str参数并且变量相同。

same_name = same_name(var_name: str)
Run Code Online (Sandbox Code Playgroud)

我循环运行它。第一次运行没问题。我第二次遇到这个错误。将变量重命名为与函数名称不同的名称修复了此问题。所以我认为这是因为Python一旦在作用域中关联了一个函数名称,第二次尝试将左侧部分(same_name =)关联为对函数的调用,并检测到str参数不存在,因此它丢失了,然后抛出该错误。


Gab*_*air 5

检查您的输入参数,并确保您没有名为type. 如果是这样,那么您将发生冲突并收到此错误。


kar*_*lli 5

str = 'Hello World String'    
print(str(10)+' Good day!!')
Run Code Online (Sandbox Code Playgroud)

即使我在上面的代码中也遇到了这个问题,因为我们正在隐藏str()函数。

解决办法是:

string1 = 'Hello World String'
print(str(10)+' Good day!!')
Run Code Online (Sandbox Code Playgroud)