current scenario -
('c' or 'a') in 'ab'
('c' or 'a') evaluates to
- the substring check could be obtained, but need to write it using
any([(i in 'ab') for i in ('c', 'a')])
- in a match case statement, one could check for exact match of a string,
x = 'ab'
case 'ab' | 'c':
but this pipe operator does not work for strings outside of match case statement.
expected scenario -
- pipe operator works for strings to check for substring in string, so,
('c' | 'a') in 'ab'
I have often found a need to test substring matching for multiple candidates. I don’t think the proposed syntax is the best solution, but I think there’s something there.
Not related to the proposal, there’s an optimisation to your second example: skipping the list creation
any((i in 'ab') for i in ('c', 'a'))
For large iterables (
('c', 'a') in this example), this means both not constructing and storing a large list (of boolean values), but also ending the iteration on the first true value found.
Also it is not clear what do you propose to be the result of this sub-expression in your example:
Should it be a special object with a special treatment of the
in operator? Can it be useful for something else?
yes that is a bit confusing, one solution is to make it a new object type, just like there is list_iterator, set_iterator, maybe
('c' | 'a') could return an object of type str_lookup.
I am investigating if it could be useful in some other place apart from the
in scenario, but in my opinion, the substring search on its own is a pretty common use case in support of this change.
alongside one another way to achieve this is by using,
if re.search('c|a', 'ab'):
where again the pipe operator is used to search the presence of any of these strings inside of
I would prefer to achieve the same without using the regex module.
there is one more way to achieve this by using a match-case statement and changing the way
__eq__ works for a string,
def __eq__(self, other):
return other in self
x = MatchString('abcdef')
case 'a' | 'b':
1 as output