0

I am learning about classes and I am messing around with them. I am trying to edit a string and reset it back to its original form. But I can't figure out how to make a 'reset' method. I tried creating a variable called 'original_string' and assigning the perimeter 'string' to it in the init method so I can simply assign self.string to original_string. I also tried creating the 'original_string' variable, outside the class. But in methods, it seems like I can't reach variables that were created outside that method. Any ideas on how to create a 'reset' method?

class Reverse:
    original_string = "Some string that will be edited"

    def __init__(self, string):
        self.string = string
        original_string = string

    def __str__(self):
        return self.string

    def reverseAll(self):
        newString = ""
        for char in self.string:
            newString = char + newString
        self.string = newString

    def reverseOrder(self):
        newString = ""
        for word in self.string.split():
            newString = str(word) + " " + newString
        self.string = newString

    def reset(self):
        #Reset the edited string back to the original
        self.string = original_string

string = Reverse("Trying to edit this string and reset it back to normal.")
print(string)
string.reverseOrder()
string.reverseAll()
string.reset()
print(string)
  • 1
    `original_string` is an attribute of your instance, hence `self.original_string`. – Ondrej K. Jan 26 '19 at 22:37
  • There is a difference between `self.original_string`, `Reverse.original_string` and `original_string`. – zvone Jan 26 '19 at 22:37
  • @DanielRoseman I think that's a bit harshly worded, but you are right. You need to replace `original_string` with `self.original_string` to make it a property. – grooveplex Jan 26 '19 at 22:37
  • Possible duplicate of [Variables inside and outside of a class \_\_init\_\_() function](https://stackoverflow.com/questions/1537202/variables-inside-and-outside-of-a-class-init-function) – TNT-Boy Jan 26 '19 at 22:53

1 Answers1

0

Just add the selfflag at the end

In [1]: class Reverse:
    ...:     original_string = "Some string that will be edited"
    ...:
    ...:     def __init__(self, string):
    ...:         self.string = string
    ...:
    ...:
    ...:     def __str__(self):
    ...:         return self.string
    ...:
    ...:     def reverseAll(self):
    ...:         newString = ""
    ...:         for char in self.string:
    ...:             newString = char + newString
    ...:         self.string = newString
    ...:
    ...:     def reverseOrder(self):
    ...:         newString = ""
    ...:         for word in self.string.split():
    ...:             newString = str(word) + " " + newString
    ...:         self.string = newString
    ...:
    ...:     def reset(self):
    ...:         #Reset the edited string back to the original
    ...:         self.string = self.original_string
    ...:
    ...: string = Reverse("Trying to edit this string and reset it back to normal.")
    ...: print(string)
    ...: string.reverseOrder()
    ...: string.reverseAll()
    ...: string.reset()
    ...: print(string)
    ...:
    ...:
Trying to edit this string and reset it back to normal.
Some string that will be edited   ```
  • I removed the variable 'original_string'. Instead, I used self.original_string = string in the init method. That way self.string was getting edited and I could simply assign self.string to self.original_string in the reset method. – Jonathan B Jan 27 '19 at 10:01