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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With