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

  • SEARCH
  • Home
  • 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 890779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:47:16+00:00 2026-05-15T13:47:16+00:00

Can someone point out to me what I’m doing wrong or where my understanding

  • 0

Can someone point out to me what I’m doing wrong or where my understanding is wrong?

To me, it seems like the code below which instantiates two objects should have separate data for each instantiation.

class Node:
    def __init__(self, data = []):
        self.data = data

def main():
    a = Node()
    a.data.append('a-data') #only append data to the a instance

    b = Node() #shouldn't this be empty?

    #a data is as expected
    print('number of items in a:', len(a.data))
    for item in a.data:
        print(item)

    #b data includes the data from a
    print('number of items in b:', len(b.data))
    for item in b.data:
        print(item)

if __name__ == '__main__':
    main()

However, the second object is created with the data from the first:

>>> 
number of items in a: 1
a-data
number of items in b: 1
a-data
  • 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-05-15T13:47:16+00:00Added an answer on May 15, 2026 at 1:47 pm

    You can’t use an mutable object as a default value. All objects will share the same mutable object.

    Do this.

    class Node:
        def __init__(self, data = None):
            self.data = data if data is not None else []
    

    When you create the class definition, it creates the [] list object. Every time you create an instance of the class it uses the same list object as a default value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone point out the math involved in getting the 3d points of an
I cant seem to get CI2's captcha helper to work... Can someone point out
I am hoping someone can point out the correct way to specialize a method
I was hoping someone can point out what my problem could be. I am
I have been looking into this for some hours now and can't figure it
Hello all I got my code and the number are finally right the only
ok, so i'm doing an assignment for a Java class and one part of
right now I have min, max value from table1 that defines the range of
I'm trying to do a raw query in SQLite. I want to select all
I'm trying to get .htaccess to rewrite urls for my site. For example: /Lessons/PhrasalVerbs/PVList/bringup

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.