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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:56:05+00:00 2026-05-16T10:56:05+00:00

I was reading this today: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#default-parameter-values and I can’t seem to understand what’s happening

  • 0

I was reading this today: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#default-parameter-values and I can’t seem to understand what’s happening under the hood.

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

The problem here is that the default
value of a_list, an empty list, is
evaluated at function definition time.
So every time you call the function,
you get the same default value. Try it
several times:

I guess first of all, when is the function definition stage? Is it an initialization stage just before the actual main function executes?

My original thinking was that the name a_list gets discarded right after the function runs so whatever [] mutated to will be garbage collected. Now, I think that a_list is not discarded at all since it’s only a name bound to the object [] so it never gets garbage collected because a_list is still bound to it. But then again, I’m wondering how I still get the same default value instead of a new []. Can someone straighten this out for me?

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-05-16T10:56:06+00:00Added an answer on May 16, 2026 at 10:56 am

    when is the function definition stage?

    Look at "Function definitions" in the Python reference:

    Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.:

    def whats_on_the_telly(penguin=None):
        if penguin is None:
            penguin = []
        penguin.append("property of the zoo")
        return penguin
    

    The parameters are evaluated when the function definition is executed. If this is in a module, it happens when the module is imported. If it’s in a class, it’s when the class definition runs. If it’s in a function, it happens when the function executes. Remember that a Python module is evaluated from top to bottom, and doesn’t automatically have an explicit "main function" like some languages.

    For example, if you put the function definition inside a function, you get a new copy of the function each time:

    >>> def make_function():
    ...     def f(value=[]):
    ...             value.append('hello')
    ...             return value
    ...     return f
    ... 
    >>> f1 = make_function()
    >>> f2 = make_function()
    >>> f1()
    ['hello']
    >>> f1()
    ['hello', 'hello']
    >>> f2()
    ['hello']
    

    The function definition creates a new function object, assigns it various properties including the code, formal parameters, and default values, and stores it in a name in the scope. Typically this only happens once for any given function, but there are cases where a function’s definition can be executed again.

    My original thinking was that the name a_list gets discarded right after the function runs so whatever [] mutated to will be garbage collected.

    Inside the body of the function, the name a_list is available to the code. So the name, and the value it is pointing to, must both still be available. It is stored in the func_defaults attribute of the function.

    But then again, I’m wondering how I still get the same default value instead of a new [].

    Because the [] is evaluated only when the function is defined, not when it is called, and the name a_list points to the same object even across repeated calls. Again, if you want the alternative behavior, use something immutable like None and check for it.

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

Sidebar

Related Questions

While reading proggit today, I came upon this comment in a post about how
I was reading Joel On Software today and ran across this quote : Without
I was reading this article today on two different regular expression algorithms. According to
I came across this code today whilst reading Accelerated GWT (Gupta) - page 151
I was reading a blog today ( http://somewebguy.wordpress.com/2009/07/20/is-encrypting-your-web-config-a-waste-of-time/ ) about both how to encrypt
Today, reading Servlet 3.0 specification, I've come across a sentence: We emphasize that this
I was reading about StreamWriter today, and came across this property, BaseStream . I
Some of what I've been reading about this yesterday and today: (SO) Should you
I've been reading a lot today about Doctrine2 usage in ZF projects and have
Reading this question I found this as (note the quotation marks) code to solve

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.