Consider a very simple function:
def generate_something(data): if data is None: raise Exception('No data!') return MyObject(data)
Its output is basically an instance of an object I want to create or an exception if the function cannot create the object. We can say that the output is binary since it either succeeds (and gives back an object) or not (and gives back an Exception).
What is the most pythonic way to handle a third state, that is "success but with some warnings"?
def generate_something(data): warnings =  if data is None: raise Exception("No data!") if data.value_1 == 2: warnings.append('Hmm, value_1 is 2') if data.value_2 == 1: warnings.append('Hmm, value_2 is 1') return MyObject(data), warnings
Is returning a tuple the only way to handle this, or it is possible to broadcast or yield warnings from within the functions and catch them from the caller?