[Python-ideas] Inline Functions - idea
Andrew Barnert
abarnert at yahoo.com
Mon Feb 10 21:39:04 CET 2014
On Feb 10, 2014, at 10:49, Alex Rodrigues <lemiant at hotmail.com> wrote:
The only reason you think you need more powerful lambdas and inline functions is that you're ignoring features Python already has. For example;
> def is_symmetric(matrix):
> element_wise(lambda: if matrix[i][j] != matrix[j][i]: return False)
> return True
This requires full statements as lambdas, and inline functions, and some kind of "multi-level return", where the implicit lambda return is level -1 so an explicit one returns from the outer (dynamic) scope, right?
But element_wise is just a genexpr in disguise. And with the builtin all function you don't need early return. And that early return was the only reason your if needed to be a statement. So:
return all(matrix[i][j] == matrix[j][i] for i, j in element_wise(matrix))
And this element_wise isn't anything magic, it's just product(range(len(matrix)), repeat=2).
More information about the Python-ideas
mailing list