How to test whether x is a member of a universal set?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping
--
Chapters
00:00 How To Test Whether X Is A Member Of A Universal Set?
00:23 Accepted Answer Score 8
00:30 Answer 2 Score 2
00:49 Answer 3 Score 0
02:43 Thank you
--
Full question
https://stackoverflow.com/questions/4876...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #math
#avk47
ACCEPTED ANSWER
Score 8
class Universe:
    def __contains__(_,x): return True
ANSWER 2
Score 2
You can inherit from the built-in list class and redefine the __contains__ method that is called when you do tests like item in list:
>>> class my_list(list):
    def __contains__(self, item):
        return True
>>> L = my_list()
>>> L
[]
>>> x = 2
>>> x
2
>>> x in L
True
ANSWER 3
Score 0
Theorem: There is no universal set.
Proof. Let X be a set such that X = {\empty, x} where x is every possible element in the domain. The question arises, is X \in X? Most sets are not defined that way, so let us define a new set Y. Y = {A \in X; A \notin A} i.e. Y is the set of all sets not belonging to themselves. 
Now, does Y \in Y? Well, we have defined Y as all sets not belonging to themselves, so Y cannot exist in Y, which contradicts our assumption.
So now assume Y is not in Y. Now A definitely contains Y, as Y is not in itself, but the definition of Y is such that if we define Y to be in Y, we contradict our own definition.
Thus, there is no set of all sets. This is known as Russell's Paradox.
So, why programmatically try to create an object that violates a result proved and tested by set theorists far more intelligent than I am? If that was my interview, this would be my answer and if they insisted it was possible, I'd suggest explaining what the problem domain is, since conceptually Russell has fundamentally proved it is impossible.
If you want a user-friendly problem usually posed for people studying introductory set theory, try the Barber Paradox.
Edit: Python lets you implement an object that contains itself. See this:
class Universal(object):
    def __init__(self):
        self.contents = []
    def add(self, x):
        self.contents.append(x)
    def remove(self, x):
        self.contents.remove(x)
    def __contains__(self, x):
        return ( x in self.contents )
However, this is not a strict set theoretic object, since the contents actually contains a reference to the parent object. If you require that objects be distinct as per the proof above, this cannot happen.