awlnx
awlnx

Reputation: 61

about socket programming in python

can someone tell me what the [0] after the function calls are? I don't know what they are called so I can't google it

buff = self.socket.recvfrom(1460)[0]
type = struct.unpack('>B', buff[0])[0]
id = struct.unpack('>l', buff[1:5])[0]

Upvotes: 0

Views: 52

Answers (1)

Andrew Johnson
Andrew Johnson

Reputation: 3186

In python the postfix [0] is short for .__getitem__(0). It gets the first item in an indexable data structure. In this case self.socket.recvfrom(1460) returns a 2-tuple of (string,address) where string is the bytes received and address is the address of the socket sending the data.

Upvotes: 1

Related Questions