python-stdlib-sentinels icon indicating copy to clipboard operation
python-stdlib-sentinels copied to clipboard

add: truthy for bool evaluation

Open legendof-selda opened this issue 2 years ago • 2 comments

As described in #12, Sentinels truthyness depends on the context of the sentinel. This allows us to set what the sentinel can mean, and by default sentinels are ambiguous and forces the user to give it a meaning.

Example


from sentinels import Sentinel

EOF = Sentinel('EOF')

# This will return a `ValueError` since EOF doesn't have a Boolean representation
# bool(EOF)
# However, we can compare a value with EOF and that does have a Boolean representation
line= read_line(file)
print(line is EOF)

Undefined = Sentinel('undefined', truthy=False)

bool(Undefined)
# >> False

Success = Sentinel('success', truthy=True)

bool(Success )
# >> True

legendof-selda avatar Sep 02 '23 13:09 legendof-selda

I haven't updated the documents yet. If this get's approved, I will update it then

legendof-selda avatar Sep 02 '23 14:09 legendof-selda

any update on this @taleinat ?

legendof-selda avatar Sep 21 '23 10:09 legendof-selda