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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:20:28+00:00 2026-06-05T06:20:28+00:00

I have a few objects in my program that have about 10-15 attributes each.

  • 0

I have a few objects in my program that have about 10-15 attributes each. These attributes are set at various places in the code in various stages in the program. As it is absolutely fine to set them on the objects without having them defined in the class in Python, there’s no good authoritative list in my program of which attributes are used.

I’d like something like the following:

class MyObject(object):
    attr1
    attr2

I’ve looked at the property() built-in, but it’s at least 6 lines of code per property. Is there an easy way to define a list of default attributes without initializing them in Python?

UPDATE: I forgot to mention, most of the values don’t have sensible defaults and would effectively be equal to None before used.

  • 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-05T06:20:31+00:00Added an answer on June 5, 2026 at 6:20 am

    I think Hugh Bothwell is on the right track, but it can be done more concisely:

    class MyClass(object):
        _defaults = "attr1", "attr2", "attr3"
        _default_value = None
    
        def __init__(self, **kwargs):
            self.__dict__.update(dict.fromkeys(self._defaults, self._default_value))
            self.__dict__.update(kwargs)
    
    
    my_object = MyClass(attr3='overridden', attr4='added')
    
    print(vars(my_object))
    

    Output:

    {'attr4': 'added', 'attr2': None, 'attr3': 'overridden', 'attr1': None}
    

    I made _defaults and _default_value class attributes (which could be changed at runtime). (Providing a mutable default value would likely require additional code to prevent it from being shared by every instance created, however.)

    This would be easy to extend to allow defining different (immutable) default values for each default attribute:

    class MyClass(object):
        _defaults = {
            "attr1": None,
            "attr2": 0,
            "attr3": ""
        }
    
        def __init__(self, **kwargs):
            self.__dict__.update(self._defaults)
            self.__dict__.update(kwargs)
    
    
    my_object = MyClass(attr3='overridden', attr4='added')
    
    print(vars(my_object))
    

    Output:

    {'attr4': 'added', 'attr2': 0, 'attr3': 'overridden', 'attr1': None}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two simple NSMutableArray that consists of few objects. Some of these objects
I have a small Python program consisting of very few modules (about 4 or
I have a program that creates an object for each file in a directory
Explanation: i have few objects and im declaring them inside $(document).ready(). WHY? because in
Suppose you have a few, quite large (100k+) objects in available and can provide
I have a few .Net projects that would benefit from using a document/object database
I have an object that I added to a few different ArrayLists. I wanna
I have few PC's that run on slow Internet connection network + they use
I have a program that loads a file (anywhere from 10MB to 5GB) a
I am relatively new to nodejs etc. Anyway I have a program that 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.