square brackets after a function call

前端 未结 3 856
执念已碎
执念已碎 2020-12-12 00:48

I am a total novice at Python, and have come across a piece of code that confuses me.

ts, pkt2 = capPort2.wait(1, 45)[0]

The previous line

3条回答
  •  难免孤独
    2020-12-12 01:18

    It means to extract the first item in the list/tuple return by the function.

    In [1]: "this is a long sentence".split()
    Out[1]: ['this', 'is', 'a', 'long', 'sentence']
    
    In [2]: "this is a long sentence".split()[0]
    Out[2]: 'this'
    

提交回复
热议问题