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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:10:21+00:00 2026-06-18T14:10:21+00:00

I was just reading post How can I assign a new class attribute via

  • 0

I was just reading post How can I assign a new class attribute via __dict__ in python? and there was one comment from @bruno desthuilliers saying:

One shouldn’t directly call magic methods – they are here as implementation of operators or generic functions. In this case, the idiomatic solution is to use setattr(obj, name, value).

The case of setattr seems out of scope of his own comment: not an operator, not really an implementation of generic function either.

Can someone explain the comment? Why should I prefer one over the other?

In which case is it preferable to use obj.__setattr__(name, value)?

  • 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-18T14:10:23+00:00Added an answer on June 18, 2026 at 2:10 pm

    You should always use setattr(), the commenter is quite correct.

    Not all types implement __setattr__, for example, yet setattr() will work correctly if the type allows the attribute to be set.

    Double-underscore methods are hooks, there to enable your custom classes to implement custom behaviour. The Python APIs will use these hooks if present, to fall back to the default behaviour if the hook is missing. By bypassing the API, you are burdening yourself with having to implement the fallback in your own code.

    You only ever need to call __setattr__ directly when implementing a subclass, and then preferably through the super() call:

    def Foo(Bar):
       def __setattr__(self, attr, value):
           # do something with attr and value
           super(Foo, self).__setattr__(attr, value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to Javascript and I was just reading following article. you can
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I'm just reading about OutputCache , and I see how you can apply VaryByParam
I was just reading an update from a friend's project, mentioning the use of
Just signed up for a trial at fogcreek.com/Fogbugz after reading Joel's latest blog post
IS there a way to post a $var to $PHP_SELF so that I can
I'm just getting started with REST and I've been reading this post and the
After reading this In The Woods blog post , I wondered: Why isn't there
I've just been reading Chad Fowler's blog post about 20 Rails Development No-Nos .
I'm new to writing template metaprogramming code (vs. just reading it). So I'm running

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.