TypeError: 'str' object is not callable (Python)

后端 未结 16 1113
忘了有多久
忘了有多久 2020-11-22 11:44

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         


        
相关标签:
16条回答
  • 2020-11-22 11:56

    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.

    0 讨论(0)
  • 2020-11-22 11:58

    In my case I had a class that had a method and a string property of the same name, I was trying to call the method but was getting the string property.

    0 讨论(0)
  • 2020-11-22 12:01

    it could be also you are trying to index in the wrong way:

    a = 'apple'
    a(3) ===> 'str' object is not callable
    
    a[3] = l
    
    0 讨论(0)
  • 2020-11-22 12:01

    I had yet another issue with the same error!

    Turns out I had created a property on a model, but was stupidly calling that property with parentheses.

    Hope this helps someone!

    0 讨论(0)
  • 2020-11-22 12:02

    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.

    0 讨论(0)
  • 2020-11-22 12:07

    Another case of this: Messing with the __repr__ function of an object where a format() call fails non-transparently.

    In our case, we used a @property decorator on the __repr__ and passed that object to a format(). The @property decorator causes the __repr__ object to be turned into a string, which then results in the str object is not callable error.

    0 讨论(0)
提交回复
热议问题