What does locals()['_[1]'] mean in Python?

后端 未结 4 599
一向
一向 2021-01-11 12:04

I saw a one liner code that is claimed to remove duplicates from a sequence:

u = [x for x in seq if x not in locals()[\'_[1]\']]

I tried th

4条回答
  •  醉话见心
    2021-01-11 12:45

    locals()['_[1]'] is a way to access a reference to list comprehension (or generator) current result inside that list comprehension.

    It is quite an evil, but can produce funny results:

    >> [list(locals()['_[1]']) for x in range(3)]
    [[], [[]], [[], [[]]]]
    

    See more details here: the-secret-name-of-list-comprehensions.

提交回复
热议问题