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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:06:22+00:00 2026-05-23T12:06:22+00:00

Python Tutorial – Important warning: The default value is evaluated only once … If

  • 0

Python Tutorial –

Important warning: The default value is evaluated only once
…
If you don’t want the default to be
shared between subsequent calls, you
can write the function like this
instead:

def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L

I still was expecting:

print f(1)    # [1]
print f(2)    # [1,2]
print f(3)    # [1,2,3]

I reason:

The default value (L=None) was executed for f(1) which helped L point to a new empty list in the fn body. However on successive calls, L=None was not executed; so L still points to the list which already has 1 in it now, and subsequent calls are simply appending more elements to it thereby sharing L.

Where am I thinking incorrectly?


UPDATE

def f(a, L=[]):
    L.append(a)
    return L

Does L here point to an empty list created in heap or stack?

  • 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-23T12:06:22+00:00Added an answer on May 23, 2026 at 12:06 pm

    L is the name of an argument, but it is also a local variable. Rebinding it rebinds the local variable, but does not change the default argument.

    UPDATE EDIT:

    Python doesn’t have “heap” and “stack” in the same manner as C; all it has are objects, and references to those objects. Your function call returns a reference to the same list that was created as the default value for the L argument of the f function, and any operation that mutates it will mutate the value of the default argument.

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

Sidebar

Related Questions

In the Python Tutorial , it says: Why? I don't see how comprehensions are
A tutorial I have on Regex in python explains how to use the re
General tutorial or good resource on how to use threads in Python? When to
in python tutorial added that python cannot hide its attributes from other classes. some
I'm following the python tutorial at their site and I'm currently at the break
I know of the few normally mentioned Python tutorial/guides/introductions, but I was wondering if
I just finished working through Google's Wave Robot: Python Tutorial . The API Reference
I'm currently going through the Lynda Python tutorial and in the section on generators
I just read the following section of the SWIG and Python tutorial: http://www.swig.org/Doc1.1/HTML/Python.html#n11 I
I have read the Google python regex tutorial about regular expression and tried to

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.