Jussi Piitulainen wrote: > Surely that should be: > > if not 'firefox' in (i.name() for i in process_iter()): > > And that again should be: > > if any((i.name() == 'firefox') for i in process_iter()):
The previous one certainly looks better than this, particularly if you move the `not` where it belongs: if 'firefox' not in (i.name() for i in process_iter()): Of course you had the next modification in mind when you suggested the above modificaton. > Which can then be made into: > > if any(i.name().startswith('firefox') for i in process_iter()): > > Or use a regex match if the condition becomes more complex. Even then, > there is re.match to attemp a match at the start of the string, which > helps to keep the expression simple. If regular expression objects were to support equality tests in the rigtht way we could stick with the containment test: >>> def process_names(): ... return (p.name() for p in process_iter()) ... >>> list(process_names()) ['foo', 'bar', 'firefox42', 'baz'] >>> >>> class Match: ... def __init__(self, regex, *args, **kw): ... self._match = re.compile(regex, *args, **kw).match ... def __eq__(self, other): ... try: ... return self._match(other) ... except TypeError: ... return False ... With the above hidden under the rug the following is quite readable: >>> Match("FIRE") in process_names() False >>> Match("FIRE", re.IGNORECASE) in process_names() True >>> Match("FIRE", re.IGNORECASE) not in process_names() False >>> Match("FIRE", re.IGNORECASE) in range(3) False -- https://mail.python.org/mailman/listinfo/python-list