我在繼承的類中覆蓋 str 時遇到了問題。有沒有辦法做類似的事情?class Sentence(str): def input(self, msg): """Extend allow to hold one changing object for various strings.""" self = Sentence(input(msg)) def simplify(self): self = self.lower() self.strip()我想更改包含在該類中的字符串,以用于各種用途。有沒有辦法做到這一點?因為我從堆棧中嘗試了很多東西,但沒有人幫助我。@編輯:有一個解釋我想做什么: 在 init 中,我初始化 Sentence 類:self.sentence = Sentence("") Mainloop,用戶可以在其中更改 Sentence:self.sentence.input("Your input:")之后我想簡化算法的字符串:self.sentence.simplify()這就是全部,之后我想使用 self.sentence 之類的字符串。但在這兩種方法中: def input(self, msg): """Extend allow to hold one changing object for various strings.""" self = Sentence(input(msg)) def simplify(self): self = self.lower() self.strip()字符串沒有改變。
從子類方法更改或覆蓋 str
慕無忌1623718
2021-10-19 16:15:49