square brackets after a function call

前端 未结 3 852
执念已碎
执念已碎 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:16

    This means that return value of the wait function is either list or tuple and 0 it is an index of the element from this output. For example:

    def func(numericValue):
        return list(str(numericValue))
    
    res = func(1000)
    res[0] - > 1
    

    Or:

    def convert(value, to_type):
        #do something
        return resuls, convertedValue
    
    res = convert(1100, str)
    res[0] - > True
    

提交回复
热议问题