On Mon, 9 May 2022 at 00:58, Valentin Berlier <[email protected]> wrote:
>
> In your example "if ([x] := foo()) is not None:" there is no possible value
> returned by foo() that could be both falsy and match the [x] pattern at the
> same time. All patterns besides the ones I listed can only be matched by
> truthy values so the work-around would only be needed for those dubious
> patterns.
>
Do you count pathological examples?
class FakeEmpty(list):
def __bool__(self): return False
borked = FakeEmpty(("zero",))
match borked:
case [x]:
if borked: print("It's iterable.", x)
else: print("Wut")
case _: print("It's not [x].")
"No possible value" ought to exclude anything that can be created with
a couple of lines of Python like this. I could accept "no
non-pathological values" for the [x] case, but are there any match
types that could unexpectedly match a falsy value in normal
circumstances?
I'm *really* not a fan of having to put "if (....) is not None" just
to work around a rare possibility, but I'm even less a fan of lurking
bugs waiting to strike (think about how datetime.time() used to be
false for midnight). Boilerplate to dodge bugs is C's job, not
Python's.
ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/YH74BMWMNS34ABEKUTFUVL3AJACJSFH7/
Code of Conduct: http://python.org/psf/codeofconduct/