The Python Oracle

Implementing the All / Universal set

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping

--

Chapters
00:00 Implementing The All / Universal Set
00:44 Accepted Answer Score 9
01:01 Thank you

--

Full question
https://stackoverflow.com/questions/2856...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #set

#avk47



ACCEPTED ANSWER

Score 9


You also need to define the reversed version of the and operator (__rand__) so your subclasses methods get called when it is the second argument as well as the first.

class UniversalSet(set):
    def __and__(self, other):
        return other

    def __rand__(self, other):
        return other