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

后端 未结 16 1345
忘了有多久
忘了有多久 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条回答
  •  -上瘾入骨i
    2020-11-22 11:53

    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.

    same_name = same_name( var_name: str)
    

    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.

提交回复
热议问题