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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:33:16+00:00 2026-06-14T22:33:16+00:00

Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument I have the following

  • 0

Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument

I have the following Python code:

class Node(object):
    def __init__(self, name, children = []):
        self.name = name
        self.children = children

    def add_child(self, child):
        self.children.append(child)

    def __str__(self):
        return self.name

def create_tree(num_vertices):
    vs = [Node(str(i)) for i in range(num_vertices)]
    for i in range(num_vertices):
        if 2 * i + 1 < num_vertices:
            vs[i].add_child(vs[2 * i + 1])
        if 2 * i + 2 < num_vertices:
            vs[i].add_child(vs[2 * i + 2])

    return vs[0]

def bfs(top_node, visit):
    """Breadth-first search on a graph, starting at top_node."""
    visited = set()
    queue = [top_node]
    while len(queue):
        curr_node = queue.pop(0)   # Dequeue
        visit(curr_node)           # Visit the node
        visited.add(curr_node)

        # Enqueue non-visited and non-enqueued children
        queue.extend(c for c in curr_node.children
                     if c not in visited and c not in queue)

def visit(tree):
    print tree

Now I make the following calls in IDLE:

>>> bfs(create_tree(3), visit)
0
1
2
>>> bfs(create_tree(3), visit)
0
1
2
1
2

Even though I’m trying to create a new tree each time, I seem to be ending up with the same tree each time (with new nodes being added each time). Why is that? In create_tree, I’m creating a new list vs with each function call.

(By the way, this is not homework. It is Exercise 4.2 in Think Complexity, which I’m reading for fun, and the exercise is not to figure out why “[e]ven though I’m trying to create a new tree each time, I seem to be ending up with the same tree each time”. (The problem is to find out why the bfs code is inefficient and I know the answer to that.))

  • 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-14T22:33:17+00:00Added an answer on June 14, 2026 at 10:33 pm

    That is because if you set in init children = [] it does not create a new list each time, it creates a list the first time and the other times uses the same one; that explains the behavior. in these cases the best thing to do is to write

    def __init__(self, ..., children = None):
        if children is None: children = []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument class Klass(object): def a(self,
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument The following code illustrates
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument I have this code
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument In the following code
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument def f(a, L=[]): L.append(a)
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following function:
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument consider the following python
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following two
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm finding that dictionary
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm trying to create

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.