Python code: TypeError: 'str' object is not callable
Missing % can cause chaos with string expansions, Sometime the print/logging statement become too huge to verify where the string is coming from.
For example in this case you might be looking for string that is used as callable forever, if you dont look at the actual string thats being expanded.
Input:
def foo1(x):
return str(x)
foo2 = lambda x: None if x is None else str(x)
def myprint(x):
print x
x = 5
myprint("x=%s, foo1(x)=%s, foo2(x)=%s" %
(str(x), foo1(x), foo2(x)))
myprint("x=%s, foo1(x)=%s, foo2(x)=%s"
(str(x), foo1(x), foo2(x)))
Output:
x=5, foo1(x)=5, foo2(x)=5
Traceback (most recent call last):
File "a.py", line 15, in <module>
(x, foo1(x), foo2(x)))
TypeError: 'str' object is not callable
For example in this case you might be looking for string that is used as callable forever, if you dont look at the actual string thats being expanded.
Input:
def foo1(x):
return str(x)
foo2 = lambda x: None if x is None else str(x)
def myprint(x):
print x
x = 5
myprint("x=%s, foo1(x)=%s, foo2(x)=%s" %
(str(x), foo1(x), foo2(x)))
myprint("x=%s, foo1(x)=%s, foo2(x)=%s"
(str(x), foo1(x), foo2(x)))
Output:
x=5, foo1(x)=5, foo2(x)=5
Traceback (most recent call last):
File "a.py", line 15, in <module>
(x, foo1(x), foo2(x)))
TypeError: 'str' object is not callable
Comments