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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:46:06+00:00 2026-05-19T13:46:06+00:00

From the docs : Many operations have an in-place version. The following functions provide

  • 0

From the docs:

Many operations have an “in-place”
version. The following functions
provide a more primitive access to
in-place operators than the usual
syntax does; for example, the
statement x += y is equivalent to x =
operator.iadd(x, y). Another way to
put it is to say that z =
operator.iadd(x, y) is equivalent to
the compound statement z = x; z += y.

Questions:

  • Why isn’t operator.iadd(x, y) equivalent to z = x; z += y?

  • How does operator.iadd(x, y) differ from operator.add(x, y)?

Related question, but I’m not interested in Python class methods; just regular operators on built-in Python types.

  • 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-19T13:46:07+00:00Added an answer on May 19, 2026 at 1:46 pm

    First, you need to understand the difference between __add__ and __iadd__.

    An object’s __add__ method is regular addition: it takes two parameters, returns their sum, and doesn’t modify either parameter.

    An object’s __iadd__ method also takes two parameters, but makes the change in-place, modifying the contents of the first parameter. Because this requires object mutation, immutable types (like the standard number types) shouldn’t have an __iadd__ method.

    a + b uses __add__. a += b uses __iadd__ if it exists; if it doesn’t, it emulates it via __add__, as in tmp = a + b; a = tmp. operator.add and operator.iadd differ in the same way.

    To the other question: operator.iadd(x, y) isn’t equivalent to z = x; z += y, because if no __iadd__ exists __add__ will be used instead. You need to assign the value to ensure that the result is stored in both cases: x = operator.iadd(x, y).

    You can see this yourself easily enough:

    import operator
    a = 1
    operator.iadd(a, 2)
    # a is still 1, because ints don't have __iadd__; iadd returned 3
    
    b = ['a']
    operator.iadd(b, ['b'])
    # lists do have __iadd__, so b is now ['a', 'b']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the docs: You usually access to-many relationships using mutableSetValueForKey:, which returns a proxy
From the isset() docs : isset() will return FALSE if testing a variable that
I'm speaking of this module: http://docs.python.org/library/operator.html From the article: The operator module exports a
From time to time I see an enum like the following: [Flags] public enum
From what information I could find, they both solve the same problems - more
How does the work_mem option in Postgres work? Here's the description from http://www.postgresql.org/docs/8.4/static/runtime-config-resource.html :
I have the unfortunate task of having to import data from excel into a
I have python2.5 and multiprocessoring (get from http://code.google.com/p/python-multiprocessing/ ) This simple code (get from
If all tables I want to delete from have the column gamer_id can i
From a web developer point of view, what changes are expected in the development

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.