Python AttributeError: 'builtin_function_or_method' 对象没有属性 'replace'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24708741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:02:45 来源:igfitidea点击:
AttributeError: 'builtin_function_or_method' object has no attribute 'replace'
提问by 1089
When I try to use this in my program, it says that there's an attribute error
当我尝试在我的程序中使用它时,它说有一个属性错误
'builtin_function_or_method' object has no attribute 'replace'
but I don't understand why.
但我不明白为什么。
def verify_anagrams(first, second):
first=first.lower
second=second.lower
first=first.replace(' ','')
second=second.replace(' ','')
a='abcdefghijklmnopqrstuvwxyz'
b=len(first)
e=0
for i in a:
c=first.count(i)
d=second.count(i)
if c==d:
e+=1
return b==e
回答by 1089
You need to callthe str.lower
method by placing ()
after it:
您需要通过在它后面放置来调用该str.lower
方法()
:
first=first.lower()
second=second.lower()
Otherwise, first
and second
will be assigned to the function object itself:
否则,first
并且second
将被分配到的函数对象本身:
>>> first = "ABCDE"
>>> first = first.lower
>>> first
<built-in method lower of str object at 0x01C765A0>
>>>
>>> first = "ABCDE"
>>> first = first.lower()
>>> first
'abcde'
>>>