Programing

TypeError : 'str'개체는 호출 할 수 없습니다 (Python).

lottogame 2020. 12. 6. 20:52
반응형

TypeError : 'str'개체는 호출 할 수 없습니다 (Python).


암호:

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'

첫 번째 Dict(z, 'w')는 작동하고 두 번째는 오류가 발생합니다.

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

이것이 왜인지 아는 사람이 있습니까?

@Greg Hewgill :

나는 이미 그것을 시도했고 오류가 발생합니다.

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

이게 문제 야:

global str

str = str(mar)

str()의미를 재정의하고 있습니다. str문자열 유형의 내장 Python 이름이며 변경하고 싶지 않습니다.

지역 변수에 다른 이름을 사용하고 global명령문을 제거하십시오 .


코드에는 없지만 %문자열 형식 지정을 시도 할 때 문자가 누락 된 또 다른 발견하기 어려운 오류가 있습니다 .

"foo %s bar %s coffee"("blah","asdf")

하지만 다음과 같아야합니다.

"foo %s bar %s coffee"%("blah","asdf")

누락 %은 동일한 TypeError: 'str' object is not callable.


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.


You can get this error if you have variable str and trying to call str() function.


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.


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.


An issue I just had was accidentally calling a string

"Foo" ("Bar" if bar else "Baz")

You can concatenate string by just putting them next to each other like so

"Foo" "Bar"

however because of the open brace in the first example it thought I was trying to call "Foo"


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

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!


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.


Whenever that happens, just issue the following ( it was also posted above)

>>> del str

That should fix it.


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.

참고URL : https://stackoverflow.com/questions/6039605/typeerror-str-object-is-not-callable-python

반응형