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

后端 未结 4 585
一向
一向 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:48

    Wow! That's nastily cryptic code. Took a bit of research to find the answer.

    Basically the list comprehension is building a new list. It names this temporary list _[1]. The locals() part is just using the locals() dictionary to look up that name, as it's not accessible otherwise. So that line is saying...

    [x for x in seq if x not in this_list]
    

    Cryptic.

    The found information on this here

提交回复
热议问题