【问题标题】:How can I reset/revert a string back to its orginal form after mutating itHow can I reset/revert a string back to its orginal form after mutating it
【发布时间】:2022-12-02 00:40:17
【问题描述】:

Alright, so I want to reset a word after I have to change/mutated it without the reset method taking any parameters. Reset() should revert the text after the use() method is used. Is there any way of doing this?

Class words

def __init__(self, text):
        self.text = text
        
    def use(self): # sets the text as an empty string
        self.text = ""

    def reset(self): # revert empty string back to the original text
 

Here is the unit test for reset()

import unittest
from word import word

def test_reset(self):
        string = word("Sunshine")
        string.use()
        string.reset()
        self.assertEqual("Sunshine", string.text)

if __name__ == "__main__":
    unittest.main()

【问题讨论】:

    标签: python


    【解决方案1】:

    I may be way out of line but... why don't you just copy it?

    Class words
    
    def __init__(self, text):
            self.text = text
            self.original = text
            
        def use(self): # sets the text as an empty string
            self.text = ""
    
        def reset(self): # revert empty string back to the original text
            self.text = self.original
    
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-02
      • 2022-12-27
      • 2022-12-27
      • 2022-12-01
      • 2022-12-02
      • 2022-12-02
      • 2022-12-02
      • 2022-12-27
      相关资源
      最近更新 更多