Python中怎么使用无穷集?

数学上的很多集合中的元素并不是有限个,在Python中怎么实现呢?

classinf_set(set):__slots__=['definition']def__init__(self,definition):self.definition=definitiondef__contains__(self,element,/):returnsuper().__contains__(element)orself.definition(element)

让我们定义一个自然数集:

N=inf_set(lambdax:all([x>=0,int(x)==x]))

运行示例如下

>>>9inNTrue>>>0inNTrue>>>-3inNFalse>>>55.9inNFalse