Code:
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'
The first Dict(z, 'w')
works and then the second time around it comes up with an error:
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
Does anyone know why this is?
@Greg Hewgill:
I've already tried that and I get the error:
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
Another case of this: Messing with the
__repr__
function of an object where aformat()
call fails non-transparently.In our case, we used a
@property
decorator on the__repr__
and passed that object to aformat()
. The@property
decorator causes the__repr__
object to be turned into a string, which then results in thestr
object is not callable error.I had the same error. In my case wasn`t because of a variable named str. But because i named a function with a str parameter and the variable the same.
I run it in a loop. The first time it run ok. The second time i got this error. Renaming the variable to a name different from the function name fixed this. So I think it´s because Python once associate a function name in a scope, the second time tries to associate the left part ( same_name =) as a call to the function and detects that the str parameter is not present, so it's missing, then it throws that error.
It is important to note (in case you came here by Google) that "TypeError: 'str' object is not callable" means only that a variable that was declared as String-type earlier is attempted to be used as a function (e.g. by adding parantheses in the end.)
You can get the exact same error message also, if you use any other built-in method as variable name.
Check your input parameters, and make sure you don't have one named
type
. If so then you will have a clash and get this error.You can get this error if you have variable
str
and trying to callstr()
function.In my case, I had a Class with a method in it. The method did not have 'self' as the first parameter and the error was being thrown when I made a call to the method. Once I added 'self,' to the method's parameter list, it was fine.