Recently I've found myself using the following idiom in some of my functions:
[...] def validate(self): # Possibly do something "expensive" to calculate whether data is valid or not if data_is_valid: return ObjectOfSomeSort(validated_data) return False ret = self.validate() if ret: return ret [...] I feel like the ret = ...; if ret: return ret syntax is a little unwieldy and unpythonic, however, and I'm not always able to do something like
if self.validate(): return self.validate() because occasionally my validation functions contain some rather computationally expensive logic.
So, StackOverflow, what python idioms exist for this sort of problem; specifically, how else can I "conditionally return"?
self.validate()implies that you want to make sure that the current(!) object, akaselfis valid.validateto returnTrueif the object is valid andFalseotherwise.