insert unique data in a list
Fire Crow
me at firecrow.com
Sun Dec 13 19:49:27 EST 2009
> Also, I'm not sure I like your abuse of the + operator to modify the
> object in place and return a flag. It is an API not shared by (as far as
> I can see) any other data type in Python.
I agree it couuld be more consisten with other object apis,
I also think that if every api has to conform to every other api
nothing will ever get done.
Heres a slightly more familiar version, it returns the value added or
none to conform with other APIs.
class unique_set(object):
def __init__(self,list):
self.list = list
def __add___(self,val):
if val not in self.list
self.list.append(val)
return val
return None
>>> unique_list = unique_set(['a','b','c'])
>>> unique_list.list
['a', 'b', 'c']
>>> unique_list + 'd'
'd'
then a test opperand to verify if a value was inserted could be
if unique_list + 'd':
# done some stuff
but it could also be used in cases like this
unique_added = unique_list + 'd' or 'not added'
More information about the Python-list
mailing list