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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:39:32+00:00 2026-05-24T07:39:32+00:00

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following function:

  • 0

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

Consider the following function:

def foo(L = []):
  L.append(1)
  print L

Each time I call foo it will print a new list with more elements than previous time, e.g:

>>> foo()
[1]
>>> foo()
[1, 1]
>>> foo()
[1, 1, 1]

Now consider the following function:

def goo(a = 0):
  a += 1
  print a

When invoking it several times, we get the following picture:

>>> goo()
1
>>> goo()
1
>>> goo()
1

i.e. it does not print a larger value with every call.

What is the reason behind such seemingly inconsistent behavior?
Also, how is it possible to justify the counter-intuitive behavior in the first example, why does the function retain state between calls?

  • 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-24T07:39:33+00:00Added an answer on May 24, 2026 at 7:39 am

    The default arguments are evaluated once when the function is defined.
    So you get the same list object each time the function is called.

    You’ll also get the same 0 object each time the second function is called, but since int is immutable, when you add 1 as fresh object needs to be bound to a

    >>> def foo(L = []):
    ...   print id(L)
    ...   L.append(1)
    ...   print id(L)
    ...   print L
    ... 
    >>> foo()
    3077669452
    3077669452
    [1]
    >>> foo()
    3077669452
    3077669452
    [1, 1]
    >>> foo()
    3077669452
    3077669452
    [1, 1, 1]
    

    vs

    >>> def foo(a=0):
    ...   print id(a)
    ...   a+=1
    ...   print id(a)
    ...   print a
    ... 
    >>> foo()
    165989788
    165989776
    1
    >>> foo()
    165989788
    165989776
    1
    >>> foo()
    165989788
    165989776
    1
    
    • 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 I'm kind of confused
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm obviously missing something
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 recently met a
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument This is very odd,
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Okay, so basically I
Possible Duplicate: Are “(function ( ) { } ) ( )” and “(function (
Possible Duplicate: Weird “406 not acceptable” error following code generate a 406 Not Acceptable
Possible Duplicate: Real differences between “java -server” and “java -client”? I've seen the argument
Possible Duplicate: In a C function declaration, what does “…” as the last parameter

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.