开发者

String building: Hiding string length?

I'm modifying python code and I came across this statement and have no idea what it means nor can I find anything on the interent about it. Sorry that its so out of context..

conn.queue.put('%x\r\n%s\r\n' % (len(chunk), chunk)
                                        if chunked else chunk)

The code is putting a chunk in a threaded queue to send at a later time. My question is what is going on here '%x\r\n%s\r\n' It appears its putting the string length then hiding it with a carriage return? Also Im confused what the 'if chunked else chunk does' as far as it being an if statemen开发者_运维问答t with no body.

Thanks


It's not an if statement with no body, it's a conditional expression. A if condition else B evaluates to A if condition is true, otherwise it evaluates to B. So in this case:

'%x\r\n%s\r\n' % (len(chunk), chunk)
                                    if chunked else chunk

Will either be:

'%x\r\n%s\r\n' % (len(chunk), chunk)

or just chunk, depending on whether chunked is true or not. The result of that is then passed to conn.queue.put.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜