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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:54:15+00:00 2026-05-23T21:54:15+00:00

I have some objects that have a dictionary of attributes, obj.attrs . The constructor

  • 0

I have some objects that have a dictionary of attributes, obj.attrs. The constructor for these objects accepts a dict and/or **kwargs, for convenience.

It looks a little like this:

class Thing:
    def __init__(self, attrs={}, **kwargs):
        for arg in kwargs:
            attrs[arg] = kwargs[arg]
        self.attrs = attrs

Such that Thing({'color':'red'}) does the same as Thing(color='red').

My problem is that the constructor somehow remembers the last attrs value passed to it.

For example:

>>> thing1 = Thing(color='red')
>>> thing2 = Thing()
>>> thing2.attrs
{'color': 'red'}

…but thing2.attrs should just be an empty dict! {}

This made me wonder if this isn’t a problem with using both **kwargs and an argument like attrs={}.

Any ideas?

  • 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-23T21:54:15+00:00Added an answer on May 23, 2026 at 9:54 pm

    The problem with using default arguments is that only one instance of them actually exists. When you say attrs={} in your init method definition, that single default {} instance is the default for every call to that method (it doesn’t make a new default empty dict every time, it uses the same one).

    The problem is that if there’s only one attrs in existence and then for every instance of Thing you say self.attrs = attrs, the self.attrs member variable for every single one of your instance is pointing to the single shared default instance of attrs.

    The other question is, isn’t this completely redundant? You can use **kwargs to pass in keyword/value args or a dictionary. If you just defined this:

    class Thing:
        def __init__(self, **kwargs):
            for arg in kwargs:
                self.attrs[arg] = kwargs[arg]
    

    All of these strategies still work:

    thing1 = Thing(color='red')
    
    thing2 = Thing(**{'color':'red'})
    
    my_dict = {'color' : 'red'}
    thing3 = Thing(**my_dict)
    

    So if you simply define and use Thing that way, you’ll avoid your problem altogether.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question with fluent interfaces. We have some objects that are used
I display some objects that have thumbnails in two ways: one in a DataGridView,
I have some business objects which use: Web.Configuration.WebConfigurationManager.AppSettings.Item(SomeSetting) Now that I'm breaking those objects
I have to take over and improve/finish some code that transforms Java objects from
I have some model objects I'm using in my Java client application. Later these
I have some custom objects and dictionaries that I want to sort. I want
I have a Dictionary<string, someobject> . EDIT: It was pointed out to me, that
I am using C# 2.0 with Nunit Test. I have some object that needs
Suppose I have a method that takes an object of some kind as an
Assume you have some objects which have several fields they can be compared by:

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.