Python - a set of one type only
Question: I want to create a Set class in Python, but unlike the set class that already
implemented in Python, I want my set to contain elements of only one type. For
example a set of integers, a set of strings and a set of any other user
defined class.

Is there any way in Python to pass the type as a parameter to the class? or
any other way to do it?

I was thinking about using dict to implement the set but dicts can contain
elements from different types.


Answer: Somethig like that?

    
    
    from sets import Set
    
    class MySet(Set):
    
        def __init__(self, iter, klass=None):
            if klass is not None:
                for item in iter:
                    if not isinstance(item, klass):
                        raise Exception("Error")
            super(MySet, self).__init__(iter) 
    
    
    if __name__ == '__main__':
    
        set1 = MySet([1,2,3], int)
        set2 = MySet([2,3,4], int)
    
        print set2.intersection(set1)
    

If you want a control of the type even in the methods like `intersection`, you
should override the call for the generation of the new Set
(`self.__class__(common)`)

bye

