-1
class EditorState:
    def __init__(self, content):
        self.content = content

class Editor:
    def __init__(self):
        self.content = ""

    def __str__(self):
        return f'{self.content}'

    def setContent(self, value):
        self.content = value

    def createContent(self):
        return EditorState(self.content)

    def restore(self, new_value):
        self.content = new_value

    def getcontent(self):
        return self.content

class History:
    def __init__(self):
        self.history = []

    def __repr__(self):
        return self.history

    def push(self, value):
        self.history.append(value)

    def remove(self):
        my_list = self.history
        my_list.pop()

        last_index = my_list[-1]
        return last_index

    def getvalue(self):
        my_list = self.history
        return self.history

editor = Editor()
history = History()

editor.setContent("a")
history.push(editor.createContent())

editor.setContent("b")
history.push(editor.createContent())

editor.setContent("c")
history.push(editor.createContent())

editor.setContent("D")
history.push(editor.createContent())

editor.restore(history.remove())

print(history.getvalue())
print(editor.getcontent())

OUTPUT that I get when I check the Items in the list: [<main.EditorState object at 0x0000017B77360040>, <main.EditorState object at 0x0000017B773600D0>, <main.EditorState object at 0x0000017B77360130>]

The output I want: [a,b,c]

I've learned how to use the Memento pattern in java, and I wanted to try the pattern with python. I does work but the problem is that when I'm returning the last item from my list in the history class, so it keeps showing me its id not the value. It's the same when I print the list using the getvalue() method.

I've tried to use the magic methods sush as str or repr but it did'nt work, also I've tried to set the attribut to a variable but no results.

  • You show classes `EditorState` and `Editor`. What do they have to do with class `History`? In class `History` method `remove` pops (removes) the last entry and then return what is now the new last entry. It is not returning what had been the last entry. Is that really what you want? And why the need to introduce a new variable `my_list`? What do you mean "it keeps showing me its id"? What is an "id"? Show us the code you are using to test this class, the result you are getting and the *precise* value you expected. – Booboo Dec 27 '20 at 16:16
  • Editor gets the value from the user and save it in the EditorState class. Then the value that is saved in the EditorState class will be added to the list that we have in the History class. The history class is used in case we will create an undo. I've created my list to just try to solve the problem, but it's actully unnecessary. – Abdullrauf Alhariri Dec 27 '20 at 19:26
  • When I use the remove method and restore method that I've created so I shold get the value that I added before my last value. Then when I call the getContent method so I should get the value, but I'm getting it's ID. <__main__.EditorState object at 0x000001EC506CF100> – Abdullrauf Alhariri Dec 27 '20 at 19:34
  • 1. You should edit the question adding the minimal code you used that resulted in the exception. 2, You should specify what the expected value from `remove` was (don't just say "the last value I added before my last value" -- actually specify the actual value, e.g. `7`, so there is no misunderstanding). 3. ideally you should include a complete stacktrace of the exception in your edited question. Here is a question for you: You start of with a newly initialized `History` instance and then do a single `push` call followed by a `remove` call. What would you expect it to return? – Booboo Dec 27 '20 at 20:04

1 Answers1

0

Fixed it :

class EditorState:
                                  #change here
    def returnContent(self,content):
        return content

class Editor():
    content = ''                  #change here
    def __init__(self):
        self.content = ""

    def __str__(self):
        return f'{self.content}'

    def setContent(self, value):
        self.content = value

    def createContent(self):
        return EditorState.returnContent(self,self.content) #Change here

    def restore(self, new_value):
        self.content = new_value

    def getcontent(self):
        return self.content

class History:
    history = []                   #change here
    def __init__(self):
        self.history = []

    def __repr__(self):
        return self.history

    def push(self, value):
        self.history.append(value)

    def remove(self):
        my_list = self.history
        my_list.pop()

        last_index = my_list[-1]
        return last_index

    def getvalue(self):
        my_list = self.history
        return my_list

editor = Editor()
history = History()

editor.setContent("a")
history.push(editor.createContent())


editor.setContent("b")
history.push(editor.createContent())

editor.setContent("c")
history.push(editor.createContent())

editor.setContent("D")
history.push(editor.createContent())

editor.restore(history.remove())

print(history.history)              #change here
print(editor.getcontent())

Output:

enter image description here

This function (in 2nd pic below) returned an object of the class instead on the variable because init() functions return only empty/None datatype(This is a Rule and the arrow mark show what datatype is being returned), so it basically returned an object which was pushed into your list

Here you can see init() returns nothing.

enter image description here

Here you can see what datatype is being pushed into the list

enter image description here

Also try creating variables globally in a class to access them easily when needed anywhere.

Ananth
  • 815
  • 1
  • 4
  • 11
  • [<__main__.EditorState object at 0x00000191FD725F10>, <__main__.EditorState object at 0x00000191FD77F0A0>, <__main__.EditorState object at 0x00000191FD77F100>]. This is the output that I'm getting. I want the values that I've added not the id. – Abdullrauf Alhariri Dec 27 '20 at 19:28
  • I'm afraid I can't be of help unless I can get to know how the other end of the code does, like what is the input, how is the object being initialized, etc. – Ananth Dec 27 '20 at 20:23
  • Done, you can see that above – Abdullrauf Alhariri Dec 28 '20 at 10:34