Python Forum
using a try/except in a conditional expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using a try/except in a conditional expression
#7
It still sounds like passing in the test by means of a function does exactly what you want. For example:

>>> def is_valid(f):
...     try:
...             return f()
...     except:
...             return False
... 
>>> def always_true():
...     return True
... 
>>> def always_throws():
...     raise Exception()
... 
>>> is_valid(always_true)
True
>>> is_valid(always_throws)
False
>>> 
(I didn't have the time to come up with a less contrived example, but hopefully you get the idea).
Reply


Messages In This Thread
RE: using a try/except in a conditional expression - by ndc85430 - Aug-31-2019, 06:41 AM

Forum Jump:

User Panel Messages

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