list - Python: I do not understand this syntax -


this question has answer here:

now learn network programming. wanted write myself recvall function. messages sent through tcp sockets , end \r\n. googling articles , blog posts, found method not understand. can written simpler?

def recvuntil(s, needle):     data = ""     while data[-len(needle):] != needle:         data += s.recv(1)     return data 

the line not understand: while data[-len(needle):] != needle:. not make sense me (but, however, works). -len(needle) should return negative number , strings numbered starting 0 ...

the expression data[n:] evaluates elements of data 1 numbered n through end of sequence. when n negative specifies position starting form right-hand end of sequence, , data[-n:] can rephrased "the last n elements of sequence."

since in case sequence string , n length of target string, read loop "while end of data string isn't target string."

it wasn't written experienced python programmer, however, since colloquial spelling be

while not data.endswith(target): 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -