开发者

python range weird behavior

I need to check a variable, called status, of an object in python. What I am doing is the known:

if p.status() in range(21,30):

where p is my object. I need to c开发者_C百科heck if this status is between -21 and -29, but it only works if I write the range in positive numbers instead of negative. When I wrote:

if p.status() in range(-30,-21):

it didn't return anything. (Of course I printed the status of this object and I am sure that this condition exist). Some ideas???

Thanks


Forget range. Chained comparison operators are much more readable.

if -29 <= p.status() <= -21:

By the way, the stop argument of range is never included in the return value, so for -21 to -29 inlusive, you'd want range(-29,-20).


You could subtract the value from 0 which would produce a positive value which would be in the range:

if 0 - p.status() in range(21,30):
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜