类型错误:';str';对象不可调用(Python)
代码:类型错误:';str';对象不可调用(Python),python,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' prin
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'
有人知道这是为什么吗
@格雷格·休吉尔:
我已经试过了,我得到了错误:
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
回溯(最近一次呼叫最后一次):
文件“C:\Users\test.py”,第63行,在
Dict(z,‘w’)
文件“C:\Users\test.py”,第53行,在Dict中
strr='''.join([c代表str中的c,如果c不在(“,”,“,”,“[”,“]),'')
TypeError:“type”对象不可编辑
这就是问题所在:
global str
str = str(mar)
您正在重新定义str()
的含义str
是字符串类型的内置Python名称,您不想更改它
为局部变量使用不同的名称,并删除
global
语句。当不在代码中时,另一个难以发现的错误是尝试设置字符串格式时缺少%
字符:
"foo %s bar %s coffee"("blah","asdf")
但它应该是:
"foo %s bar %s coffee"%("blah","asdf")
缺少
%
将导致相同的类型错误:“str”对象不可调用,我试图调用该方法,但得到了string属性。这种情况的另一种情况是:在调用format()
失败的情况下,弄乱了对象的\uuu repr\uu
函数
在我们的例子中,我们在\uuu repr\uuu
上使用了@property
装饰器,并将该对象传递给格式()
。@property
装饰器会将\uuu repr\uuu
对象转换为字符串,然后导致str
对象不可调用错误。我也有同样的错误。在我的例子中,不是因为名为str
的变量。但是因为我用str参数和变量命名了一个函数
same_name = same_name(var_name: str)
我循环运行它。第一次运行正常。第二次我犯了这个错误。将变量重命名为与函数名不同的名称修复了此问题。所以我认为这是因为Python一次在一个作用域中关联一个函数名,第二次尝试将左部分(same_name=
)关联为对函数的调用,并检测到str
参数不存在,所以它丢失了,然后抛出错误。在我的例子中,我有一个类,其中有一个方法。该方法没有将“self”作为第一个参数,并且在我调用该方法时引发了错误。一旦我将“self”添加到方法的参数列表中,就可以了。如果您使用变量str
并尝试调用str()
函数,则会出现此错误。我刚才遇到的一个问题是不小心调用了字符串
"Foo" ("Bar" if bar else "Baz")
您可以通过这样将字符串彼此相邻来连接它们
"Foo" "Bar"
但是,由于第一个示例中的大括号是开的,所以它认为我试图调用“Foo”
,也可能是您试图以错误的方式进行索引:
a = 'apple'
a(3) ===> 'str' object is not callable
a[3] = l
我又遇到了一个同样错误的问题
事实证明,我在模型上创建了一个属性,但却愚蠢地用括号调用该属性
希望这对别人有帮助 检查输入参数,确保没有名为type
的参数。如果是这样的话,您将发生冲突并出现此错误。需要注意的是(如果您是通过Google来到这里的话),“TypeError:“str”对象不可调用”仅表示尝试将先前声明为字符串类型的变量用作函数(例如,通过在末尾添加paranthes)
如果您使用任何其他内置方法作为变量名,您也可以得到完全相同的错误消息。每当发生这种情况时,只需发出以下消息(它也在上面发布)
这应该可以解决问题
str = 'Hello World String'
print(str(10)+' Good day!!')
甚至我在跟踪str()
函数时也遇到了上述代码的问题
解决办法是:
string1 = 'Hello World String'
print(str(10)+' Good day!!')
我从未完成的方法检查中得到此警告:
if hasattr(w, 'to_json'):
return w.to_json()
######### warning, 'str' object is not callable
它假设w.to_json
是一个字符串。解决方案是添加一个callable()
检查:
if hasattr(w, 'to_json') and callable(w.to_json):
然后警告消失。建议不要使用str
int
列表
等。。作为变量名,即使python允许。
这是因为当试图访问名为相同的保留关键字时,它可能会造成这样的事故。我只是在一个稍微不同的用例中遇到了这个问题。我搜索了又搜索我的代码,寻找我可能在哪里使用了'str'变量,但找不到它。我开始怀疑我导入的模块中可能有一个是罪魁祸首。。。但遗憾的是,在格式化的print语句中缺少“%”字符
下面是一个例子:
x=5
y=6
print("x as a string is: %s. y as a string is: %s" (str(x) , str(y)) )
这将导致输出:
TypeError: 'str' object is not callable
更正如下:
x=5
y=6
print("x as a string is: %s. y as a string is: %s" % (str(x) , str(y)) )
导致我们的预期产出:
x as a string is: 5. y as a string is: 6
重命名str
变量的所有实例,包括第53行中该错误所抱怨的实例。不打算使用该变量,但是,str()
函数也可以作为\uuuuuuuu内置项\uuuuuuuuuu.str()
使用。当然,我想在99.9999%的情况下使用它是一个坏主意。请记住,您需要重新启动python内核以获得返回的str()
功能。@yeliabsalohcin,如果您意外地覆盖了它,并且内置功能将被恢复,您只需del str
。我声明了一个变量(str=1)并且得到了错误。因此,我不必删除str.就可以使str()再次工作。无论如何,这是一个很棒的提示!谢谢你,先生!简单的错误,但影响很大,破坏了一个脚本。我知道这真的很旧,但我刚刚遇到了这个问题,这个答案很有帮助,谢谢@naxaThis在一个小时的故障排除后救了我。谢谢谢谢,这就是在我的例子中导致相同错误的原因。具有相同名称的属性和方法,用字符串覆盖该方法,然后尝试执行该方法/字符串也会导致TypeError:“str”对象不可调用谢谢,这也是我的问题(我没有使用
x=5
y=6
print("x as a string is: %s. y as a string is: %s" % (str(x) , str(y)) )
x as a string is: 5. y as a string is: 6