Search code examples
pythonpython-2.7encapsulationshallow-copy

Python Encapsulation: overwriting variables with-in a class


Background:

  • I am using python 2.7.4
  • I am reading a document line by line into the class
  • I want to have two variables within a class
  • One variable(I made it an array called lines) I want to have all the original formatting of the document
  • The other variable(I also made into an array called linesfixed), I want to get rid of indentation and line wrap characters "-"

The Issue:

My setLinesFixed() method is for some reason modifying both my lines(not desired) and linesfixed(desired) variables... I am not sure what the problem is, but I feel like it is some sort of encapsulation issue.

Code in class:

def __init__(self):
    self.lines = []  # sets up the empty array for lines without modifications
    self.linesfixed = []  #sets up the empty array for lines with indentation and line wraps gone

def setLines(self,realfilename):
    tempfile = open(realfilename)
    self.lines = tempfile.readlines() # this sets the self.lines correctly
    tempfile.close()
    # print self.lines gives the desired output >> ["blah blah.. -","blah blah"]

def setLinesFixed(self,templines):
    self.linesfixed = templines 
    index = len(self.linesfixed)-1
    while index >= 0:
        cleanline = self.linesfixed[index].strip()
        if(cleanline == ""):
            self.linesfixed.pop(index)
        elif((cleanline[-1] == "-")and(cleanline[0] != ";")):
            cleanline = cleanline[:-1]
            temp = self.linesfixed.pop(index+1)
            temp = temp.strip()
            self.linesfixed[index] = cleanline + temp
        else:
            self.linesfixed[index] = self.linesfixed[index].strip()
        index = index - 1
    # print self.linesfixed gives me the desired output for fixing lines >> ["blah blah.. blah blah"]
    # print self.lines gives me the same result as self.lines fixed >> ["blah blah.. blah blah"]

Example Input:

self.setLines(["blah blah.. -","blah blah"])
self.setLinesFixed(self.lines)

Actual Output:

self.lines = ["blah blah.. blah blah"]
self.linesfixed = ["blah blah.. blah blah"]

Desired Output:

self.lines = ["blah blah.. -","blah blah"]
self.linesfixed = ["blah blah.. blah blah"]

What is wrong here? and why does self.lines get modified as well as self.linesfixed? Is this an encapsulation issue?


Solution

  • The problem is that you're not copying the array before modifying it, you're just copying a reference to the array. After this line:

    self.linesfixed = templines
    

    Both self.linesfixed and templines refer to the same array. To get the intended behavior, you need to clone the array:

    self.linesfixed = list(templines)