As mentioned in the comments, the best way to do this is to simply return your function to a constant number of values, and if your use case is actually more complex (e.g. parsing arguments), use a library for it.
However, in your question, I explicitly asked you to use the Pythonic method to handle functions that return a variable number of arguments, and I believe that this can be done using decorators . They are not very common, and most people tend to use them more than creating them, so here's a tutorial on creating decorators. Learn more about them.
Below is a decorated function that does what you are looking for. The function returns an iterator with a variable number of arguments and is filled to a certain length in order to better accommodate the unpacking of the iterator.
def variable_return(max_values, default=None):
Which outputs what you are looking for:
0 None None 0 1 None 0 1 2
The decorator basically takes a decorated function and returns its result, along with sufficient additional values ββto fill max_values . Then the caller can assume that the function always returns exactly the max_values number of arguments and can use fancy decompression, as usual.
source share