2015-03-02 53 views
-2

如何在類中使用'默認'方法,例如:string.lower()如何在類中應用python方法?

這是我的錯誤:

result = text.lower.replace(string.punctuation, ' ').split(' ') 
AttributeError: MyClass instance has no attribute 'lower' 

回答

2

你缺少一個更一套()

result = text.lower().replace(string.punctuation, ' ').split(' ') 
       ^
+0

我添加了大括號,但仍然出現錯誤。結果='text.lower()。replace(string.punctuation,'').split('') AttributeError:MyClass實例沒有屬性'lower'' – Omicron 2015-03-02 14:47:10

+0

什麼是'text'?它是'str'嗎?你可以向「isWordIn」顯示你的電話嗎? – CoryKramer 2015-03-02 14:54:55

+0

文字是類似新聞標題的字符串''考拉熊是柔軟和可愛'''類TitleTrigger(WordTrigger): def evaluate(self,text): return(self.isWordIn(text))' – Omicron 2015-03-02 15:00:12

1

()lower方法缺失。

a.lower將返回函數對象。

例如

>>> a = "ABCf" 
>>> a.lower 
<built-in method lower of str object at 0xb7047ac0> 
>>> a.lower() 
'abcf' 
相關問題