[Python-ideas] add __contains__ into the "type" object
Clint Hepner
clint.hepner at gmail.com
Thu Mar 2 07:57:06 EST 2017
> On 2017 Mar 2 , at 2:53 a, Stephan Houben <stephanh42 at gmail.com> wrote:
>
> A crucial difference between a set and a type is that you cannot
> explicitly iterate over the elements of a type, so while we could implement
>
> x in int
>
> to do something useful, we cannot make
>
> for x in int:
> print(x)
>
__contains__ was introduced to provide a more efficient test than to simply
iterate over the elements one by one. I don’t see why something has to be iterable
in order to implement __contains__, though.
class PositiveInts(int):
def __contains__(self, x):
return x > 0
N = PostiveInts()
> Because if we could, we could implement Russell's paradox in Python:
>
> R = set(x for x in object if x not in x)
>
> print(R in R)
object is not the equivalent of the paradoxical set of all sets. It’s closer to the set of
all valid Python values. That includes all valid Python set values, but a Python
set is not mathematical set; it’s a *finite* collection of *hashable* values.
More information about the Python-ideas
mailing list