开发者

How to test whether x is a member of a universal set?

开发者 https://www.devze.com 2023-02-08 17:23 出处:网络
I have a list L, and x in L evaluates to True if x is a member of L. What can I use 开发者_开发技巧instead of L in order x in smth will evaluate to True independently on the value of x?

I have a list L, and x in L evaluates to True if x is a member of L. What can I use 开发者_开发技巧instead of L in order x in smth will evaluate to True independently on the value of x?

So, I need something, what contains all objects, including itself, because x can also be this "smth".


class Universe:
    def __contains__(_,x): return True


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消