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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:34:30+00:00 2026-06-14T23:34:30+00:00

I’m coding a TreeStructure (TS) class, which lets me create parent and child objects,

  • 0

I’m coding a TreeStructure (TS) class, which lets me create parent and child objects, which are linked to each other. Every TS -object has m_parent attribute, which is controlled with parent property, and also they have children list, which holds in all the children of this parent. Whenever I add a child to it’s parent’s children list, it gets added to it’s own children list too? Here’s what I have:

ROOT = "__ROOT__"

class TreeStructure:

    def __init__(self, parent=ROOT, children=[]):
        self.children = children
        self.parent = parent

    @property
    def parent(self):
        '''Returns m_parent'''
        if hasattr(self, "m_parent"):
            return self.m_parent
        else:
            return None

    @parent.setter
    def parent(self, parent=ROOT):
        '''Sets m_parent'''
        if type(parent) == type(self):
            if self.parent:
                del self.parent
            self.m_parent = parent
            self.m_parent.children.append(self)
        elif parent == ROOT:
            if self.parent:
                del self.parent
            self.m_parent = ROOT
        else:
            raise TypeError("Parent's type %s did not match objects type %s"
                            %(type(parent), type(self)))

    @parent.deleter
    def parent(self):
        '''Deletes m_parent'''
        if self.parent:
            if self.parent != ROOT:
                self.m_parent.children.remove(self)
            del self.m_parent

And now by creating two simple objects, it should work. However, it doesn’t.

a = TreeStructure()
b = TreeStructure(a)

The problem appears at line 25, self.m_parent.children.append(self). If I add print’s to both sides of that line, I see that both print(self.m_parent.children) and print(self.children) print an empty list [] BEFORE the append line. Now if I add the prints AFTER the append line, both prints will say [<__main__.TreeStructure object at 0x...>], which should only happen for the parent, not the child?

  • 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-14T23:34:33+00:00Added an answer on June 14, 2026 at 11:34 pm

    Do not use [] as a default value!

    >>> def bad(default=[]):
    ...     default.append(1)
    ...     print default
    ... 
    >>> bad()
    [1]
    >>> bad()
    [1, 1]
    >>> bad()
    [1, 1, 1]
    >>> def good(default=None):
    ...     if default is None:
    ...             default = []
    ...     default.append(1)
    ...     print default
    ... 
    >>> good()
    [1]
    >>> good()
    [1]
    

    Default arguments are created when the function is defined, not when it is called.
    So only use non-mutable types for defaults. using integers, strings, tuples is okay, but if you want a default list or dictionary or anything mutable then use None and do the above trick.

    Read this question and answers for better understanding of this matter.

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

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I

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.