Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:55:51+00:00 2026-06-07T13:55:51+00:00

I want to append different courses into two different classrooms, however it keeps adding

  • 0

I want to append different courses into two different classrooms, however it keeps adding the same courses to both of the classrooms.
This is my AddCourse function

Edited

I modified my code according to your suggestions.

class Classroom:
    """I omitted part of the class for brevity"""
    def __init__(self, Seed = None, ClassroomId = None, FirstCourseStartTime = None, LastCourseEndTime = None, CourseList = [], ProfessorList = []):
        self.setFirstCourseStartTime(FirstCourseStartTime)
        self.setLastCourseEndTime(LastCourseEndTime)
        self.setCourseList(CourseList)
        self.setProfessorList(ProfessorList)
        self.setSeed(Seed)
        self.setClassroomId(ClassroomId)
    def addCourse(self, Course):
        self.CourseList.append(Course)
    def setCourseList(self, List):
        self.CourseList = List
#the statements below are from a different file to run the code/class above    
Classroom1 = Classroom(Seed = os.urandom(1024/8), FirstCourseStartTime = 8, LastCourseEndTime = 19.75)
Classroom2 = Classroom(Seed = os.urandom(1024/8), FirstCourseStartTime = 8, LastCourseEndTime = 19.75)
# Adding the courses to the classrooms
Classroom1.addCourse(Course0)
Classroom1.addCourse(Course1)
Classroom1.addCourse(Course2)
Classroom1.addCourse(Course3)
Classroom1.addCourse(Course4)
Classroom1.addCourse(Course5)
Classroom1.addCourse(Course6)
Classroom1.addCourse(Course7)
Classroom2.addCourse(Course8)
Classroom2.addCourse(Course9)
Classroom2.addCourse(Course10)
Classroom2.addCourse(Course11)
Classroom2.addCourse(Course12)
Classroom2.addCourse(Course13)
Classroom2.addCourse(Course14)
Classroom2.addCourse(Course15)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:55:53+00:00Added an answer on June 7, 2026 at 1:55 pm

    It looks like your issue is that all instances of your class are dealing with the same list, since you are declaring it only once.

    Here’s an example of your problem, without all the extra stuff that doesn’t matter:

    class Classroom(object):
    
        def __init__(self, course_list=[]):
            self.course_list = course_list
    
        def add_course(self, course):
            self.course_list.append(course)
    

    When we use it, we see the behavior you described:

    c1 = Classroom()
    c2 = Classroom()
    
    c1.add_course('ENG 101')
    c2.add_course('MATH 101')
    
    print c1.course_list
    print c2.course_list
    

    Prints:

    ['ENG 101', 'MATH 101']
    ['ENG 101', 'MATH 101']
    

    To show that this is the same list, we can print the id of each instance’s list:

    print id(c1.course_list)
    print id(c2.course_list)
    

    For me, this outputs this (though it likely different each time):

    4299912760
    4299912760
    

    The problem line for you is course_list=[] in __init__‘s param list. You’re defining the list there, then likely not copying it anywhere else. This means whenever you append to the list, it’s always the same list you are appending to. It’s important to understand that, in python, the method def line (not the body, obviously), is only run once, not every time you create a new instance (or run the function, for that matter).

    Here’s a corrected version. Instead of defaulting to [], you should most always default to None then handle setting it as a list of nothing was given.

    class Classroom2(object):
    
        def __init__(self, course_list=None):
            self.course_list = course_list or []
    
        def add_course(self, course):
            self.course_list.append(course)
    
    
    c1 = Classroom2()
    c2 = Classroom2()
    
    c1.add_course('ENG 101')
    c2.add_course('MATH 101')
    
    print c1.course_list
    print c2.course_list
    

    Now this prints as expected:

    ['ENG 101']
    ['MATH 101']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I want to append the contents of a src file into the end
I want to apply a 3 argument function in different ways based on a
Here is the function that works: /* $(#right-sidebar).click(function() { $(this).append(<div class='editable'>hello world</div>); $(.editable).css(background-color,red); });
I want to do two async request from two different kml file, so I
if i have both encrypted message and signature (let's say two different size files),
i want to append unlimited number of HTML elements with data given as comment.
I want to append .item element before .content element but it just simply removes
I want to append a div inside a div which have many divs inside
I want to append a record in a TClientDataSet based on another record (in
I want to append a <br /> to a particular class. Using the :after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.