How to update Class Variable that is constantly changing

I am trying to update the end variable in my SuffixNode Class, automatically. What I mean is I have created a SuffixNode in the following code, and I assigned the endIndex.end as the SuffixNode's end value. Then I update the endIndex.end to 2. However, when I print the (self.root.end) out after I updated the endIndex.end value, the end value store in SuffixNode is still showing 1 rather than show the updated 2.

Can anyone provide me with a suggestion on how should I modify the code, so that when I update the endIndex.end, the end value store in the SuffixNode will also update automatically.

Thank you

Below is the code

class EndIndex: def init(self, endIndexValue): self.end = endIndexValue

class ActivePoint:
    def __init__(self, activeLength, activeNode, activeEdge, suffixCount):
        self.activeLength = activeLength
        self.activeNode = activeNode
        self.activeEdge = activeEdge
        self.suffixCount = suffixCount


class SuffixNode:
    def __init__(self, start, end, index = None):
        # Create a list storing all the possible english alphabet
        # This will be used to store the starting characte
        self.children = [None] * 87

        # The pointer to the other node via suffix link
        self.suffixLink = None

        # The index of the start and end of the substring
        self.index = index
        self.start = start
        self.end = end


class SuffixTree:
    def __init__(self):
        # Initiate Active Point Values the End Index Value
        activePoints = ActivePoint(0, 0, 0, 0)
        endIndex = EndIndex(0)

        print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, endIndex.end))

        endIndex.end = 1

        print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, endIndex.end))

        self.root = SuffixNode(0, endIndex.end)

        print(self.root.end)


        endIndex.end = endIndex.end + 1

        print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, endIndex.end))

        print(self.root.end)


suffixTree = SuffixTree()

Upvotes: 0

Views: 138

Answers (2)

DragonBobZ
DragonBobZ

Reputation: 2463

When you instantiate SuffixNode to self.root, self.root.end is assigned the current value of endIndex.end. self.root.end is not a reference to endIndex.end, it's just a duplicate of the integer that endIndex.end contained at the assignment. Now, if you assigned self.root = endEndex, self.root.end would be shown to change when endIndex.end changes.

Upvotes: 1

Anwarvic
Anwarvic

Reputation: 13022

You don't need to create endIndex in your code at all. And that's the only change that you need to make. So, your SuffixTree should be like that:

class SuffixTree:
    def __init__(self):
        # Initiate Active Point Values the End Index Value
        activePoints = ActivePoint(0, 0, 0, 0)
        self.root = SuffixNode(0, 0)  #<---- defing root here with 0 as a start value for end

        print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, self.root.end)) #<---- use self.root.end instead

        self.root.end = 1  #<---- use self.root.end instead of endIndex.end

        print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, self.root.end)) #<---- use self.root.end instead

        print(self.root.end)


        self.root.end += 1  #<---- use self.root.end instead of endIndex.end

        print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, self.root.end)) #<---- use self.root.end

        print(self.root.end)

Upvotes: 1

Related Questions