scope - Python set won't perform union -
i have class member defined:
self.myset = set()
when perform myobject.myset.add('item')
, using instance of class, works fine: print(myobject.myset)
gives me {'item'} .
however, when perform myobject.myset.union(yourset)
, yourset
not empty, won't work; print(myobject.myset)
still prints empty set. why 1 method (add
) working while (union
) quietly (no exception thrown) failing? myobject.myset = yourset
works, union
in particular won't.
i'm using python 3.
set.union()
not modify old set in-place, set.add()
does. set.union()
returns new set instead.
myobject.myset = myobject.myset.union(yourset)
this should trick. see python documentation on set.union()
more information.
Comments
Post a Comment