Search code examples
pythonset-intersection

Implementing intersection operator for a set-like class in Python


I'm trying to implement a wrapper class which should ideally allow me to get the intersection of its elements using the notation:

a & b

Is there a specific method that I can implement to achieve this? (I know that the individual elements need to implement the __hash__ and __eq__ methods)

I'm currently getting the following error:

TypeError: unsupported operand type(s) for &: 'PropArray' and 'PropArray'

Solution

  • Try to override:

    def __and__(self, *args, **kwargs): # real signature unknown
        """ Return self&value. """
        pass
    

    in your class