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

后端 未结 16 1356
忘了有多久
忘了有多久 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:53

    I got this warning from an incomplete method check:

    if hasattr(w, 'to_json'):
        return w.to_json()
                 ######### warning, 'str' object is not callable
    

    It assumed w.to_json was a string. The solution was to add a callable() check:

    if hasattr(w, 'to_json') and callable(w.to_json):
    

    Then the warning went away.

提交回复
热议问题