Keep getting the ID from items in a list which is in a class

84 Views Asked by At
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.

1

There are 1 best solutions below

3
Ananth On BEST ANSWER

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.