3 ms·
A clean solution would be arity of return values, optionally with a ≥ constraint on the original number of returned values vs bound variable names. E.g. a funct
by nothrabannosir 8y ago
A clean solution would be arity of return values, optionally with a ≥ constraint on the original number of returned values vs bound variable names. E.g. a function returning two values can be bound to zero, one, or two variables, but not three. A function returning 1 can be bound to only 1 or none, and if you return no values, you cannot be bound to anything.
Aside: it is a common misconception that Python has multiple return values: it does not. It can return a tuple, which can be automatically destructured into multiple values:
a, b = (1, 2) # this could have been a function
and it has syntactic sugar for tuples which allows leaving off the parantheses:
>>> 1, 2
(1, 2)
Which, combined, makes it look like multiple return values:
def foo():
return 1, 2
but, it really isn't:
>>> foo()
(1, 2)
>>> type(foo())
<type 'tuple'>
so this would be hard to achieve in Python. :(
- coldtea 8y agoWell, couldn't it be like "no return inside the function body" --> no LHS assignment allowed?
- nothrabannosir 8y agoSure :) but it would require a deep change in Python language semantics, without which this isn't possible. You'd have to carry that information in the AST somehow. From a call site, a function object is just a an object with certain properties. "Did it have a return or not?" is not information which exists on that object. Only the return value is (when you call it), and if it is None, you don't know if that came from "return None" or from the default behaviour. Python could enrich the function object to include that information, and it would essentially be "supporting 0 or 1 return values", instead of "n return values". But adding that no-assignment rule would be backwards incompatible. At that point, might as well go all the way and support any number return values :)