Return on negative condition vs if block

If I have a function that needs to perform an action on some condition and instead returns null, which is cleaner:

def func(): if not condition: return None [...] return asd 

or

 def func(): if condition: [...] value = asd else: value = None return value 

I read the paradigm that each function should have one return point. On the other hand, zen python says that a plane is better than a nested one, and a later version adds one nested level over the entire action code (marked as [...]).

Also note that conditions can be more complex and, for example, add more than one level of nesting.

+6
source share

All Articles