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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:15:10+00:00 2026-05-24T18:15:10+00:00

I am trying to build a tree structure that represents a parsed configuration file

  • 0

I am trying to build a tree structure that represents a parsed configuration file (the configuration file has a hierarchical structure to it). I represented this as:

class configContainer():

    treeDict = {}
    instances = {}

class configObject():

    def __init__(self, name, configLine, parent):
        self.name = name                # Assign to the line number to make unique
        self.parent = parent            
        self.children = []
        self.configLine = configLine    # Actual line of the configuration
        configContainer.instances[name] = self

The configContainer contains a set of objects. configContainer.instances uses a key of “line#” to map to the object. treeDict does a similar mapping, but with a different key (I create treeDict after the entire container is created).

I then try to reference two objects inside two different configContainers. This works fine from __main__. But when I pass the two configContainers to a function, instances always returns objects from configContainer2

if __name__ == "__main__":


    f1 = open('rfile', 'r')
    configFile1 = f1.read()
    f1.close()

    configTree1 = parseConfig(configFile1)
    configTree1.treeDict = createTreeDict(configTree1)
    zObject1 = configTree1.instances["line10"]


    f2 = open('sfile', 'r')
    configFile2 = f2.read()
    f2.close()

    configTree2 = parseConfig(configFile2)
    configTree2.treeDict = createTreeDict(configTree2)
    zObject2 = configTree2.instances["line10"]

    print "\n\nFrom __main__"
    print "###########################"
    print configTree1
    print configTree2
    print zObject1
    print zObject2


    compareConfigurations(configTree1, configTree2)


def compareConfigurations(tmpTree1, tmpTree2):

    print "\n\nFrom compareConfigurations"
    print "###########################"
    print tmpTree1
    print tmpTree2
    zObject1 = tmpTree1.instances["line10"]
    zObject2 = tmpTree2.instances["line10"]
    print zObject1
    print zObject2

Result is:

### From __main__

<__main__.configContainer instance at 0xb77a34ec>

<__main__.configContainer instance at 0xb740a68c>

<__main__.configObject instance at 0xb740e3ac>

<__main__.configObject instance at 0xb7414bcc>


### From compareConfigurations

<__main__.configContainer instance at 0xb77a34ec>

<__main__.configContainer instance at 0xb740a68c>

<__main__.configObject instance at 0xb7414bcc>

<__main__.configObject instance at 0xb7414bcc>

I can’t figure out why I am always getting back the 0xb7414bcc object from inside compareConfigurations?

  • 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-24T18:15:11+00:00Added an answer on May 24, 2026 at 6:15 pm

    configContainer.instances is a class attribute, so if you modify it for any instance of a class it will change for all instances of that class. With your current code any time you create a new configObject with the same name it will overwrite the entry in configContainer.instances for that name. You should either make instances an instance attribute of configContainer or make sure your configObjects have different names.

    class configContainer():
        def __init__(self):
            self.instances = {}
    ...
    

    Here is a quick example of what is happening:

    >>> cc1 = configContainer()
    >>> cc2 = configContainer()
    >>> cc1.instances["line10"] = "foo"
    >>> configContainer.instances
    {'line10': 'foo'}
    >>> cc2.instances["line10"] = "bar"
    >>> configContainer.instances
    {'line10': 'bar'}
    >>> cc1.instances
    {'line10': 'bar'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a Quadtree data structure(or let's just say a tree)
I am trying to build an Eclipse application that would work with a linux/motif
I am trying to create a tree structure with graphviz. I am open to
I am trying to build a dynamic Expression Tree like below : Func<IEnumerable<int>, int,
I am attempting to dynamically build up an expression tree so that I can
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
I trying to build and compile my xcodeproj in command line and it is
I'm trying to build a grammar with the following: NUMERIC: INTEGER | FLOAT |
I'm currently trying to build a personal website to create a presence on the
I am trying to build a function in C/C++ to sort an array and

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.