Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

math

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".

like image 452
psihodelia Avatar asked Feb 02 '11 15:02

psihodelia


2 Answers

class Universe:
    def __contains__(_,x): return True
like image 94
Fred Foo Avatar answered Nov 14 '22 23:11

Fred Foo


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
like image 33
Emmanuel Avatar answered Nov 14 '22 22:11

Emmanuel