0

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]
4
  • This doesn't have too much to do with socket programming per se. Commented Aug 10, 2014 at 23:19
  • It's called "indexing", or occasionally (as in the formal grammar) "subscription", both in Python and in a wide variety of other languages. And you should read a tutorial, at least far enough to learn how to index sequences, before trying to write socket code. Commented Aug 10, 2014 at 23:32
  • As a side note, type and id are built-in functions (well, actually, a class and a function), so it's generally not a good idea to hide them with variables of the same name. (As opposed to the more common case of naming a variable list, you're less likely to need these particular functions in everyday code—but it's still confusing if someone asks you to, e.g., print the type of some value and you get an exception saying that int is not callable…) Commented Aug 10, 2014 at 23:34
  • I didn't write it myself. I pulled a class for interfacing with minecraft servers off of dinnerbone. Commented Aug 11, 2014 at 0:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.