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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:22:41+00:00 2026-05-23T01:22:41+00:00

I am a python learner and currently hacking up a class with variable number

  • 0

I am a python learner and currently hacking up a class with variable number of fields as in the “Bunch of Named Stuff” example here.

class Bunch:
    def __init__(self, **kwds):
        self.__dict__.update(kwds)

I also want to write a __setattr__ in this class in order to check the input attribute name. But, the python documentation says,

If __setattr__() wants to assign to an
instance attribute, it should not
simply execute “self.name = value” —
this would cause a recursive call to
itself. Instead, it should insert the
value in the dictionary of instance
attributes, e.g., “self.__dict__[name]
= value”.
For new-style classes, rather than
accessing the instance dictionary, it
should call the base class method with
the same name, for example,
“object.__setattr__(self, name,
value)”.

In that case, should I also use object.__dict__ in the __init__ function to replace self.__dict__?

  • 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-23T01:22:41+00:00Added an answer on May 23, 2026 at 1:22 am

    No. You should define your class as class Bunch(object), but continue to refer to self.__dict__.

    You only need to use the object.__setattr__ method while you are defining the self.__setattr__ method to prevent infinite recursion. __dict__ is not a method, but is an attribute on the object itself, so object.__dict__ would not work.

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

Sidebar

Related Questions

Python gives us the ability to create 'private' methods and variables within a class
Python, through it's readline bindings allows for great command-line autocompletion (as described in here
I've been an avid learner of the Python language for quite some time. Having
Hey guys, I'm currently completing Zed Shaw's Learn Python the Hard Way and I've
I learned that Python class attributes are like static data members in C++. However,
I learned that when executing commands in Python, I should use subprocess. What I'm
I have no knowledge of Python. I started with .NET and than learned PHP.
First, let me say that I'm a complete beginner at Python. I've never learned
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python works on multiple platforms and can be used for desktop and web applications,

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.