[Tutor] Quick question

Kent Johnson kent37 at tds.net
Fri Sep 21 21:23:28 CEST 2007


Tino Dai wrote:
> Is there a more pythonic way of doing this:
> 
>   if queuePacket.has_key('procSeq') and \
>   queuePacket.has_key('opacSeq') and \
>   queuePacket.has_key('keySeq') and \
>   len(queuePacket['procSeq']) == 0 and \
>   len(queuePacket['opacSeq']) == 0 and \
>  len(queuePacket['keySeq']) == 0:

'procSeq' in queuePacket is a little better.

You could put all the tests in a loop with all():

if all( (key in queuePacket and len(queuePacket[key]) == 0)
     for key in ('procSeq', 'opaqSeq', 'keySeq')):

You could replace the test with something like
len(queuePacket.get(key, 'xxx'))==0

which I think is equivalent though maybe a bit obscure...

Kent


More information about the Tutor mailing list