开发者

String building: Hiding string length?

开发者 https://www.devze.com 2023-04-01 05:08 出处:网络
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..

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

精彩评论

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