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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:08:08+00:00 2026-06-10T12:08:08+00:00

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument As I was reading

  • 0

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

As I was reading the following handout: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables

I came across the following example. Basically, it claims that:

def bad_append(new_item, a_list=[]):
    a_list.append(new_item)
    return a_list

is not the best way to append items to a list since a_list is evaluated at at function definition time.

Instead, a better alternative is:

def good_append(new_item, a_list=None):
    if a_list is None:
        a_list = []
    a_list.append(new_item)
    return a_list

since it defines the variable in the function’s runtime.

Coming from a C-background, isn’t a_list a local variable here? How does it store its value from one function call to the other? Furthermore, can someone please elaborate on why the second example is better than the first? What’s wrong with defining functions in the definition? It doesn’t seem like it overwrites the original value or anything.

Thanks!

  • 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-10T12:08:10+00:00Added an answer on June 10, 2026 at 12:08 pm

    A simple answer is that the def statement creates an object, and at the objects execution (when the def line is executed), a new list object is created and attached to the object (in this case, the object is a function).

    At subsequent execution – what you might be expecting is that a new list is generated; but in reality the same object that was created when the function definition was first run sticks around; and all “members” of that object remain the same. That is why it keeps adding to the same list.

    The reason it is considered bad is because what you might expect from the argument (that at every execution, a new list is generated) is not what the statement does. This is why the better pattern is to pass None or some other blank object.

    Consider the following:

    >>> bad_append('foo')
    ['foo']
    >>> bad_append('bar')
    ['foo', 'bar']
    >>> bad_append('zoo')
    ['foo', 'bar', 'zoo']
    >>> bad_append('bar',[])
    ['bar']
    >>> bad_append('bar') # what?
    ['foo', 'bar', 'zoo', 'bar']
    

    As you can see when I passed in a new list, at the next execution it doesn’t stick around; but rather the original list that was built when the def was executed remains.

    For more information – see this excellent explanation from effbot.

    • 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 Consider the following function:
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
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I was working on
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 Edit: This has nothing
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm kind of confused
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm very confused about
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm obviously missing something

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.