Python Forum
suggested change for all() and any() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: suggested change for all() and any() (/thread-12018.html)



suggested change for all() and any() - Skaperen - Aug-05-2018

the all() and any() functions test all the elements of the given iterator (list, etc) to see if all/any of those elements are True. my suggested change is to support one or more optional additional arguments. then the logic is to test if all/any of the elements of the iterator given in the first argument is equal to any of the remaining arguments. if there is only one argument, the original logic is performed. if two arguments are given, where the 2nd argument is True, theses functions act similar to the way if only one argument is given, but the test is for exactly the value True. IOW, the default for the remaining arguments, if none are, given, is the generalized test for truthfulness (the original logic).

discussion?

* Skaperen ducks his head!


RE: suggested change for all() and any() - ichabod801 - Aug-06-2018

Changing built-in functions should not be done lightly, it should return considerable utility. Considering how easy it is to do what you want, I only see minimal utility gains.

knights = ['Sir Robin', 'Sir Galahad', 'Sir Bedevere', 'King Arthur']
if filter(lambda item: item in knights, people):
    # your desired 'any' functionality.
if len(filter(lambda item: item in knights, people)) == len(people):
    # your desired 'all' functionality.
I didn't even have to import anything.


RE: suggested change for all() and any() - Skaperen - Aug-07-2018

i guess we each see it differently. i do agree such changes should not be done lightly. the big downside i see is that my suggestion locks out other future feature additions by using up all the arguments. but that can be worked around with all the values in a tuple in the 2nd argument.