I just recently came across this cool hack in Python.
This:
d = {}
for i, d[i] in enumerate('abc'):
pass
>>> d
{0: 'a', 1: 'b', 2: 'c'}
>>>
This assigns key value pairs to a empty dictionary from the iterator.
I would like to know how Cython backend parses this, my expectation is that it's being parsed with unpacking assignment. But it would be nice to know the actual Cython implementation of this, and also if doing this is recommended or not?
I know I just can simply do:
d = {}
for i, v in enumerate('abc'):
d[i] = v
But the cool hack above can do this with shorter code, but I am not sure if it is considered good practice in Python.
I never seen anybody use this...