Python Forum
suggested change for all() and any()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
suggested change for all() and any()
#1
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!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  suggested improvements. Skaperen 0 1,391 Mar-27-2020, 12:06 AM
Last Post: Skaperen
  want suggested module for e-mail parsing Skaperen 0 1,485 Jul-26-2019, 08:52 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020