3 ms·
This one is a bit more subtle. Assume there are two classes on that uses @property and one that does not. class Here: @property def boolean
by tgbugs 6y ago
This one is a bit more subtle. Assume there are two classes on that uses @property and one that does not.
class Here:
@property
def booleanPredicate(self):
return random_bool()
class There:
def anotherBooleanPredicate(self):
return random_bool()
here = Here()
there = There()
What happens in the following cases?
here.booleanPredicate # as expected
here.booleanPredicate() # loud failure
here.anotherBooleanPredicate # silently always True
here.anotherBooleanPredicate() # as expected
So you might think that you should _always_ use @property for boolean predicates, except that there are libraries that use the anotherBooleanPredicate version so you are now at risk for silent always True behavior (creeping sanity destroying evil behavior). To prevent the silent failure you should always start by calling the predicate so that it can fail loudly. Worse if someone does a refactor and misses going from @property form to function form then the silent failure literally cannot be detected by running the program (!).