0

So this has been confusing me for a couple of days now, I have three classes one called Page:

class Page:

   def __init__(self, pageName, sectionIBelongTo="Uncategorised"):
        self.mySection = sectionIBelongTo
        #each page belongs to only one section
        self.name = pageName

Which must have an assigned section object:

class Section:

    childPages = []

    def __init__(self, padName):
        self.name = padName

    def addSection(self, pageObject):
        self.childPages.append(pageObject)

Section also has a list of all child notes. This is all managed through a single Book class object:

class Book:
    Sections = []

    def __init__(self):
        print "notebook created"

    def addSection(self, secName):
        sectionToAdd = Section(secName)

        self.Sections.append(sectionToAdd)

    def addPage(self, bufferPath, pageName, pageSection="Uncategorised"):
        #Create a page and add it to the appropriate section


        for section in self.Sections:
            if section.name == pageSection:
                sectionToSet = section
        #Search list of sections for matching name.

        newPage = Page(pageName, sectionToSet)
        #Create new page and assign it the appropriate section object

        self.Sections[self.Sections.index(sectionToSet)].addSection(newPage)
        #Add page to respective section's list of pages.

Which as you can see holds a list of all of its sections. So I import these classes from another file and try to fill up my book like so:

myBook = Book()

myBook.addSection("Uncategorised")
myBook.addSection("Test")
myBook.addSection("Empty")
#Create three sections

myBook.addPage("belongs to uncategorised")
#Add page with no section parameter (uncategorised).
myBook.addPage("Belongs to test", "Test")
#Add page to section "Test"
myBook.addPage("Belongs to uncategorised again")
#Another uncategorised page

for x in range(0, 3):
    print "Populated section '", myBook.Sections[x].name, "', with: ", len(myBook.Sections[x].childPages), " child pages."

The output shows that all three sections are created fine, but every section has 3 child pages, if I print the names of the pages it appears that every page has been added to every section.

I would hugely appreciate anyone being able to spot my silly mistake.

Thanks in advance! :)

1 Answer 1

1

Making childPages an instance attribute, not a class attribute, should solve your problem:

class Section:

    def __init__(self, padName):
        self.name = padName
        self.childPages = []

    def addSection(self, pageObject):
        self.childPages.append(pageObject)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.