I found a bug in python set.update(set())

Documentations says that set can not have mutable elements but when i am trying to add set to the set using update method it is getting executed without throwing any error.

pls confirm if i am correct else explain the reason of the same.

1 Like

set.update() does not “add” the passed in parameter to itself. It assumes it to be an iterable and adds all of the elements produced by the iterable to the set.

Do you mean this?

>>> a = {1, 2, 3, 4}
>>> b = {4, 5}
>>> a.update(b)
>>> a
{1, 2, 3, 4, 5}

What’s the problem? None of the elements of b are mutable.

A=set()
B=A.update(A)

Set is mutable but cannot contain mutable elements
Then why A.update() gets executed without throwing any error whereas i m trying to add a mutable element which is a set to the set

You are not trying to add mutable element with this:

>>> s = set('abc')
>>> s
{'a', 'b', 'c'}
>>> s.update([1, 2, 3])
>>> s
{1, 2, 'c', 3, 'b', 'a'}

If you try to add mutable item it will raise an error:

>>> s.update([{1}])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
1 Like