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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:45:15+00:00 2026-06-16T18:45:15+00:00

Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument I have something quite

  • 0

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

I have something quite strange when I tried to write a class like below. On the line 3, I must put a new copy of the argument newdata to the self.data, otherwise, it seems that when I initiate a new class instance, the value of the previous one is still remembered by the class. see the example below and please pay attention to the only difference in the two versions of code on line 3.

class Pt(object):                                                               
    def __init__(self,newdata={}):                                              
        self.data=newdata.copy()                                                       
        if self.data == {}:                                                     
            self._taglist = []                                                  
        else:                                                                   
            self._taglist = self.data.keys()                                    
    def add_tag(self,tag=None):                                                 
        self.data[tag]={'x':[0,1,2,3,4]}                                        
        self._taglist.append(tag)                                               


In [49]: pt = Pt()

In [50]: pt.add_tag('b')

In [51]: pt.add_tag('a')

In [52]: pt.data
Out[52]: {'a': {'x': [0, 1, 2, 3, 4]}, 'b': {'x': [0, 1, 2, 3, 4]}}

In [53]: pt2 = Pt()

In [54]: pt2._taglist
Out[54]: []

class Pt(object):                                                               
    def __init__(self,newdata={}):                                              
        self.data=newdata                                                       
        if self.data == {}:                                                     
            self._taglist = []                                                  
        else:                                                                   
            self._taglist = self.data.keys()                                    
    def add_tag(self,tag=None):                                                 
        self.data[tag]={'x':[0,1,2,3,4]}                                        
        self._taglist.append(tag)                                               

In [56]: pt = Pt()

In [57]: pt.add_tag('a')

In [58]: pt.add_tag('b')

In [59]: pt._taglist
Out[59]: ['a', 'b']

In [60]: pt2 = Pt()

In [61]: pt2._taglist
Out[61]: ['a', 'b']

In [62]: pt2.data
Out[62]: {'a': {'x': [0, 1, 2, 3, 4]}, 'b': {'x': [0, 1, 2, 3, 4]}}

I guess the second case happens because: both newdata and self.data refer to the same object (but how could this happen, the value should be given from right to the left but not reverse), so when I use the “add_tag” method to update the self.data, the newdata is updated as well. Yet I think when I initiate a new instance by pt2 = Pt(), the newdata should use the default value ({}), can it still keeps the old value from pt1?

  • 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-16T18:45:16+00:00Added an answer on June 16, 2026 at 6:45 pm

    The why and all is already explained very well in “Least Astonishment” in Python: The Mutable Default Argument. So here is just a quick help how to fix your problem.

    As default parameter objects are kept, you should never use mutable objects as default parameter values. Instead, you should define a fixed value, usually None, as the default value for which then the default object is initialized. So your constructor would look like this:

    def __init__(self, newdata=None):
        if newdata is None:
            newdata = {}
        # ...
    
    • 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 class Klass(object): def a(self,
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 seeing some strange
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument I have the following
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument I have this code
Possible Duplicate: Least Astonishment in Python: The Mutable Default Argument List extending strange behaviour
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 def f(a, L=[]): L.append(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.