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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:15:36+00:00 2026-05-16T06:15:36+00:00

I have just tried the following in Python 2.6: >>> foo = (set(),) >>>

  • 0

I have just tried the following in Python 2.6:

>>> foo = (set(),)
>>> foo[0] |= set(range(5))
TypeError: 'tuple' object does not support item assignment
>>> foo
(set([0, 1, 2, 3, 4]),)
>>> foo[0].update(set(range(10)))
>>> foo
(set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),)

I have several questions here:

  • Why does foo[0] |= set(range(5)) update the set and throw an exception?
  • why does foo[0].update(set(range(10))) work without a problem? Should it not have the same result as the first statement?

Edit Many people have pointed out, that tuples are immutable. I am aware of that. They have also pointed out, that |= would create a new set object and assign it to the tuple. That is wrong. See this:

>>> foo = set()
>>> bar = foo
>>> foo is bar
True
>>> foo |= set(range(5))
>>> foo
set([0, 1, 2, 3, 4])
>>> bar
set([0, 1, 2, 3, 4])
>>> foo is bar
True

This means that no new object has been created, but the existing one was modified. This should work with the tuple. Please note also that, although my first code throws a TypeError, the set within the tuple is still updated. That is the effect I am interested in. Why the TypeError, when the operation obviously was successful?

  • 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-16T06:15:36+00:00Added an answer on May 16, 2026 at 6:15 am
    >>> def f():
    ...   x = (set(),)
    ...   y = set([0])
    ...   x[0] |= y
    ...   return   
    ... 
    >>> import dis
    >>> dis.dis(f)
      2           0 LOAD_GLOBAL              0 (set)
                  3 CALL_FUNCTION            0
                  6 BUILD_TUPLE              1
                  9 STORE_FAST               0 (x)
    
      3          12 LOAD_GLOBAL              0 (set)
                 15 LOAD_CONST               1 (0)
                 18 BUILD_LIST               1
                 21 CALL_FUNCTION            1
                 24 STORE_FAST               1 (y)
    
      4          27 LOAD_FAST                0 (x)
                 30 LOAD_CONST               1 (0)
                 33 DUP_TOPX                 2
                 36 BINARY_SUBSCR       
                 37 LOAD_FAST                1 (y)
                 40 INPLACE_OR          
                 41 ROT_THREE           
                 42 STORE_SUBSCR        
    
      5          43 LOAD_CONST               0 (None)
                 46 RETURN_VALUE        
    

    This shows that the statement x[0] |= y is implemented by calling x[0].__ior__(y) and then assigning the returned value to x[0].

    set implements in-place |= by having set.__ior__ return self. However, the assignment to x[0] still takes place. The fact that it’s assigning the same value that was already there is irrelevant; it fails for the same reason that:

    x = (set(),)
    x[0] = x[0]
    

    fails.

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

Sidebar

Related Questions

I have tried using a stemmer but the words it produces are just not
Let's say I have the following Python code: if conditionOne(): if conditionTwo(): foo =
I have just tried adding a WebService (asmx based) from a WebForms project to
I have just tried using the NSArchiver but for some reason I am getting
I have just tried to uploaded an app through the Application Loader, but it
Update I have just tried Pinning my site to the Taskbar again (after removing
I have tried just about everything for getting the Kinect for Windows to actually
I have just downloaded the flexigrid and examples and tried to execute. In the
I tried implementing a sorting program in mapreduce such that I have just the
just,I tried to remove the node from the xml file what i have.But it

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.