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 6981173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:07:19+00:00 2026-05-27T18:07:19+00:00

I’m relatively new to python (but not to programming), and I can’t explain the

  • 0

I’m relatively new to python (but not to programming), and I can’t explain the following behavior. It appears that a variable (the list “children” in my example) from one object (“child”) is getting overwritten by the value for that variable in a completely different object (“node”). To give some context, I’m trying to create a simple Node class to use in a tree structure. The node has children and a parent (all other nodes).

I can’t figure out why child.children gets the same value as node.children. Are they somehow referencing the same data? Why? Code and output follows:

class Node:
    children = []
    parent = 0
    visited = 0
    cost = 0
    position = (0, 0)
    leaf = 0

    def __init__(self, parent, pos):
        self.parent = parent
        self.position = pos

    def addChild(self, node):
        self.children += [node]

node = Node(0, (0,0))
child = Node(node, (3,2))

node.addChild(child)

print "node: ",
print node

print "node.childen: ",
print node.children

print "child: ",
print child

print "child.children",
print child.children

Output:

node:  <__main__.Node instance at 0x414b20>
node.childen:  [<__main__.Node instance at 0x414b48>]
child:  <__main__.Node instance at 0x414b48>
child.children [<__main__.Node instance at 0x414b48>]

As you can see, both node.children and child.children have the same value (a list containing child) even though I only updated node.children. Thanks for any help!

  • 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-27T18:07:20+00:00Added an answer on May 27, 2026 at 6:07 pm

    The children variable was declared as a class-level variable so it is shared amongst all instances of your Nodes. You need to declare it an instance variable by setting it in the initializer.

    class Node:
        #children = [] # not here...
        parent = 0     # might want to rethink where you initialize these
        visited = 0
        cost = 0
        position = (0, 0)
        leaf = 0
    
        def __init__(self, parent, pos):
            self.parent = parent
            self.position = pos
            self.children = [] # ...but here
    
        def addChild(self, node):
            self.children += [node]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.