[Python-Dev] Re: containment and the empty container

2021年11月08日 19:28:42 -0800

Let's use a concrete example: `re.RegexFlag`
```
Help on function match in module re:
match(pattern, string, flags=0)
 Try to apply the pattern at the start of the string, returning
 a Match object, or None if no match was found.
```
In use we have:
 result = re.match('present', 'who has a presence here?', 
re.IGNORECASE|re.DOTALL)
Inside `re.match` we have `flags`, but we don't know if `flags` is nothing (0), a single flag (re.ASCII, maybe) or a group of flags (such as in the example). For me, the obvious way to check is with:
 if re.IGNORECASE in flags: # could be re.I in 0, re.I in 2, re.I in 5, etc.
 ...
Now, suppose for the sake of argument that there was a combination of flags 
that had its own code path, say
 weird_case = re.ASCII | re.LOCALE | re.MULTILINE
I can see that going two ways:
 weird_case in flags # if other flags allowed
or
 weird_case is flags # if no other flags allowed
The idiom that I'm shooting for is using `in` to check for flags:
- flag1 in flag1 True
- flag1 in (flag1 | flag2) True
- flag3 in (flag1 | flag2) True
- flag3 in flag1 False
- flag3 in flag4 False
and
- flag0 in any flag False
- any flag in flag0 False
And, of course, if we want to know if the thing we have is exactly flag1:
 flag is flag1
will tell us.
Does this make sense?
--
~Ethan~
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/7P552C66N6S3OBLH4UPWINU7TVPFT2Y3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to