I want to create a collection namedtuplein python, with the ability to dynamically add elements using a join operation.
The following code snippet creates setfrom namedtuplewhich behaves beautifully.
from collections import namedtuple
B = namedtuple('B', 'name x')
b1 = B('b1',90)
b2 = B('b2',92)
s = set([b1,b2])
print(s)
which prints
{B(name='b1', x=90), B(name='b2', x=92)}
Now, if I create another one namedtupleand add it to mine setusing operations union, it will not behave as expected.
b3 = B('b3',93)
s = s.union(b3)
print(s)
The code snippet prints the following output.
{93, B(name='b1', x=90), B(name='b2', x=92), 'b3'}
The expected result should be:
{B(name='b1', x=90), B(name='b2', x=92), B(name='b3', x=93)}
Am I misunderstanding the API? Both python2 and 3 show the same behavior.