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 8151287

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:20:04+00:00 2026-06-06T15:20:04+00:00

I tried to create dynamic object to validate my config in fly and present

  • 0

I tried to create dynamic object to validate my config in fly and present result as object. I tried to achieve this by creating such class:

class SubConfig(object):
    def __init__(self, config, key_types):
        self.__config = config
        self.__values = {}
        self.__key_types = key_types

    def __getattr__(self, item):
        if item in self.__key_types:
            return self.__values[item] or None
        else:
            raise ValueError("No such item to get from config")

    def __setattr__(self, item, value):
        if self.__config._blocked:
            raise ValueError("Can't change values after service has started")

        if item in self.__key_types:
            if type(value) in self.__key_types[item]:
                self.__values[item] = value
            else:
                raise ValueError("Can't assing value in different type then declared!")
        else:
            raise ValueError("No such item to set in config")

SubConfig is wrapper for section in config file. Config has switch to kill possibility to change values after program started (you can change values only on initialization).

The problem is when I setting any value it is getting in infinity loop in getattr. As I read __getattr__ shouldn’t behave like that (first take existing attr, then call __getattr__). I was comparing my code with available examples but I can’t get a thing.

I noticed that all problems are generated my constructor.

  • 0 0 Answers
  • 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-06T15:20:06+00:00Added an answer on June 6, 2026 at 3:20 pm

    Cf ecatmur’s answer for the root cause – and remember that __setattr__ is not symetrical to __getattr__ – it is unconditionnaly called on each and every attempt to bind an object’s attribute. Overriding __setattr__ is tricky and should not be done if you don’t clearly understand the pros and cons.

    Now for a simple practical solution to your use case: rewrite your initializer to avoid triggering setattr calls:

    class SubConfig(object):
        def __init__(self, config, key_types):
            self.__dict__.update(
                _SubConfig__config=config,
                _SubConfig__values={},
                _SubConfig__key_types=key_types
                )
    

    Note that I renamed your attributes to emulate the name-mangling that happens when using the double leading underscores naming scheme.

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

Sidebar

Related Questions

I tried to create this by following the video at http://www.asp.net/learn/videos/video-7026.aspx where Joe Stagner
I tried to create a file using Oracle UTL_FILE.FOPEN command but i get this
This is the first Python script I've tried to create. I'm reading a xml
in order to test a managed class I tried to create an instance in
I've got a need to create a dynamic proxy in C#. I want this
I tried to create object using ComAutomationFactory.CreateObject . It is giving following exception Failed
Is it possible to create dynamic embed function in ActionScript3 for example like this
i'm pretty new to scala. I've tried to create a kind of dynamic repository.
I tried to create a Style for DataGridTextColumn with the following code <Style TargetType={x:Type
I tried to create a new Swing GUI desktop application template using NetBean 7.0

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.