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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:30:36+00:00 2026-05-28T18:30:36+00:00

#!/usr/bin/env python3.2 def f1(a, l=[]): l.append(a) return(l) print(f1(1)) print(f1(1)) print(f1(1)) def f2(a, b=1): b

  • 0
#!/usr/bin/env python3.2

def f1(a, l=[]):
    l.append(a)
    return(l)

print(f1(1))
print(f1(1))
print(f1(1))

def f2(a, b=1):
    b = b + 1
    return(a+b)

print(f2(1))
print(f2(1))
print(f2(1))

In f1 the argument l has a default value assignment, and it is only evaluated once, so the three print output 1, 2, and 3. Why f2 doesn’t do the similar?

Conclusion:

To make what I learned easier to navigate for future readers of this thread, I summarize as the following:

  • I found this nice tutorial on the topic.

  • I made some simple example programs to compare the difference between mutation, rebinding, copying value, and assignment operator.

  • 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-28T18:30:37+00:00Added an answer on May 28, 2026 at 6:30 pm

    This is covered in detail in a relatively popular SO question, but I’ll try to explain the issue in your particular context.


    When your declare your function, the default parameters get evaluated at that moment. It does not refresh every time you call the function.

    The reason why your functions behave differently is because you are treating them differently. In f1 you are mutating the object, while in f2 you are creating a new integer object and assigning it into b. You are not modifying b here, you are reassigning it. It is a different object now. In f1, you keep the same object around.

    Consider an alternative function:

    def f3(a, l= []):
       l = l + [a]
       return l
    

    This behaves like f2 and doesn’t keep appending to the default list. This is because it is creating a new l without ever modifying the object in the default parameter.


    Common style in python is to assign the default parameter of None, then assign a new list. This gets around this whole ambiguity.

    def f1(a, l = None):
       if l is None:
           l = []
    
       l.append(a)
    
       return l
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +
The following test fails: #!/usr/bin/env python def f(*args): >>> t = 1, -1 >>>
My code: #!/usr/bin/env python def Runaaall(aaa): Objects9(1.0, 2.0) def Objects9(aaa1, aaa2): If aaa2 !=
I run python CGIHTTPServer with (server.py) #!/usr/bin/env python import CGIHTTPServer def main(): server_address =
The following code: #!/usr/bin/env python import mechanize class MechanizeSubclass(mechanize.Browser): def __init__(self, factory=None, history=None, request_class=None,
!/usr/bin/env perl use warnings; use strict; my $text = 'hello ' x 30; printf
Here is my script #!/usr/bin/env ruby if __FILE__ == $0 `cd ..` puts `ls`
Which version would you prefer? #!/usr/bin/env perl use warnings; use strict; use 5.010; my
I have an awk script that I have defined thus: #!/usr/bin/env awk BEGIN {
Are both of these versions OK or is one of them to prefer? #!/usr/bin/env

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.