On Nov 18, 2019, at 13:14, Random832
On Mon, Nov 18, 2019, at 13:35, Serhiy Storchaka wrote:
18.11.19 04:39, Daniel Zeng пише:
Syntax for tuple comprehension, something like: (i, for i in range(10))
(**(i for i in range(10)))
It'd be (*(i for i in range(10)),) but point taken. In fact, after thinking about it some more, I'm not sure if there's any more efficient way to implement it anyway, considering that there's no way for the loop that would be generated to append to a tuple.
At the C API level, tuples are mutable, and this is safe to use as long as you’re sure no Python code has a reference to the tuple. So there’s no reason we couldn’t have a special TUPLE_APPEND op that’s only used in tuple comprehensions, the same way LIST_APPEND is used in list comprehensions. (In a comprehension you can be sure that no Python code has a reference to the object that you’re building until after you’ve finished building it.) Of course this is all CPython-specific; you’d have to think about how, say, PyPy could implement the feature before declaring that it’s definitely not a problem. But I _think_ it’s not a problem.