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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:22:33+00:00 2026-06-13T13:22:33+00:00

Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument class Klass(object): def a(self,

  • 0

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

class Klass(object):

    def a(self, d={}):
        print d
        self.b(d)

    def b(self, d={}):
        import random
        print d
        d[str(random.random())] = random.random()

I assumed that every time I call c.a() without arguments, I got a fresh empty dict. However, this is what actually happens:

>>> c = Klass()
>>> c.a()
{}
{}
>>> c.a()
{'0.637151613258': 0.61491180520119226}
{'0.637151613258': 0.61491180520119226}
>>> c.a()
{'0.637151613258': 0.61491180520119226, '0.960051474644': 0.54702415744398669}
{'0.637151613258': 0.61491180520119226, '0.960051474644': 0.54702415744398669}
...

I don’t really want to do some sort of lambda/iscallable thing. What is a good pattern to use here?

Of course, I figured out I decent way around the problem by the time I had started typing out the question. But, if anyone has a different pet way around this, I would love to hear.

  • 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-13T13:22:35+00:00Added an answer on June 13, 2026 at 1:22 pm

    Could get a fresh, empty dict with:

        def a(self, d=None):
            if d is None:
                d = {}
    

    The problem is well described here under Mutable Default Arguments.

    • 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 def f(a, L=[]): L.append(a)
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument from random import random
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'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 I want to understand
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 kind of confused
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Edit: This has nothing
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Python list confusion I

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.