[Python-ideas] Predicate Sets

Steven D'Aprano steve at pearwood.info
Tue Jan 21 02:07:26 CET 2014


On Sun, Jan 19, 2014 at 11:56:49PM -0800, Andrew Barnert wrote:

> And your suggestion has the exact same problem that naive set theory had:

> >>> russellset = predicateset(lambda s: s not in s)
> >>> russellset in russelset
> 
> Presumably this should cause the computer to scream "DOES NOT 
> COMPUTE!" and blow up, which I think would be hard to implement in 
> CPython.

It should just raise an exception. I leave implementation as an exercise 
for the reader :-)

This sort of thing is a staple of bad old science fiction, where the 
Hero would save the world by getting the super-intelligent Artificial 
Intelligence Doomsday Computer to calculate some variation of the above. 
But of course, a *truely* intelligent computer would merely say "I see 
what you did there. Good try, feeble meatbag, but not good enough" and 
launch the missiles.


> The big problem is coming up with a compelling use case. This one doesn't sell me:
> 
>     bar_files = search_files('bar', exclude=predicateset(lambda fname: not fname.endswith('~'))) 

If it's a project on PyPI, the only use-case necessary is the author 
thinks it's cool.


> It seems like it make more sense to have exclude take a function, so you could just write:
> 
>     bar_files = search_files('bar', exclude=lambda fname: not fname.endswith('~'))

What if you want to filter according to multiple conditions? A tuple of 
functions makes sense. Add a helper function that tests against those 
multiple functions, and you're halfway to this PredicateSet. Adding 
set-like methods seems like overkill.


-- 
Steven


More information about the Python-ideas mailing list