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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:23:21+00:00 2026-06-07T08:23:21+00:00

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

  • 0

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

Consider the following two functions

def a(var=[0]):
    print (var)
    var = var + [4]

def b(var=[0]):
    print (var)
    var.append(4)

They should act the same, but more importantly, both should simply print ‘[0]’ if called with no arguments. The act very differently and only a() will print ‘[0]’ all the time.

>>> a()
[0]
>>> a()
[0]
>>> a()
[0]

>>> b()
[0]
>>> b()
[0, 4]
>>> b()
[0, 4, 4]

Why does a() function differently from b()?

It seems like you can’t use the list API if you want the variable to be overwritten with the default in the case that no arguments are past to the function. And if you do the function will ‘remember’ what it the variable was previously.

The situation in my code appears in a recursive function, so just ‘del’-ing the unwanted variable won’t really work. Is there a way to overwrite a variable every time you call the function with no arguments?

After hours and hours of research I discovered this. It might be related to the question above and might lead to an answer. We can define a lifetime class like so:

class lifetime:   # adapted from http://naml.us/blog/tag/variable-lifetime
    def __init__(self, name):
        self.name = name
        print ('creating: ' + name)
    def __del__(self):
        print ('destroying: ' + self.name)
    def __iadd__(self, a):
        self.append(a)
    def append(self, a):
        self.name += ' and ' + a.name
        print('appending: ' + a.name + ' to ' + self.name)

and then define 2 functions:

def a(var=lifetime('a')):
    print (var)
    var += life('appendage')

def b(var=lifetime('b')):
    print (var)
    var.append(life('appendage'))

>>> a()
<__main__.lifetime object at 0x00000000031FFA90>
creating: appendage
appending: appendage to a and appendage
destroying: appendage
>>> a()
<__main__.lifetime object at 0x00000000031FFA90>
creating: appendage
appending: appendage to a and appendage and appendage
destroying: appendage
>>> b()
<__main__.lifetime object at 0x00000000031FFB38>
creating: appendage
appending: appendage to b and appendage
destroying: appendage
>>> b()
<__main__.lifetime object at 0x00000000031FFB38>
creating: appendage
appending: appendage to b and appendage and appendage
destroying: appendage

It seems like it just evaluates the default for the argument once, and then uses whatever the evaluation for that was. It never says, ‘creating: a’ or ‘creating: b’. So maybe if it were to evaluate the default argument each time, it would lead to an answer to the actual question.

  • 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-07T08:23:23+00:00Added an answer on June 7, 2026 at 8:23 am

    Because the two code snippets do two inherently different things. var + [4] creates a new list consisting of the contents of the list of var with 4 added, we then assign this new list to var. var.append(4) on the other hand appends the number 4 to the existing list.

    Your problem is that var=[0] is only evaluated once and then reused, since your append changes the variable itself you’ll see the effect on later invocations. The other code only assigns a new object to a local variable which won’t have an effect later.

    Usually you don’t use modifiable defaults but instead write:

    def foo(var=None):
        if var is None: var = [0]
    

    If you need to remember data from earlier invocations, put the whole function into a class.

    • 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 def f(a, L=[]): L.append(a)
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 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
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 recently met a

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.