开发者

Python Lambda with Or

Reading the documentation it seems this might not be possible, but it seems that a lot of people have been able to beat more complicated functionality into pythons lambda function.

I'm leveraging the scapy libraries to do some packet creation. Specially this questions is about the ConditionalField which takes it a field a开发者_JAVA技巧nd a comparison function, the field only being added to the packet if the comparison is true, but I need to do 2 comparisons.

Example with only one check, this works:

ConditionalField(XShortField("chksum",None),lambda pkt:pkt.chksumpresent==1)

What I want:

ConditionalField(XShortField("chksum",None),lambda pkt:pkt.chksumpresent==1 or (lamba pkt:pkt.special == 1))

This isn't giving expected results. Is there a way to do this?


lambda pkt:((pkt.chksumpresent == 1) or (pkt.special == 1))


Is lambda the most readable/maintainable? The following is just as performant:

def checksum_condition(pkt):
    return pkt.chksumpresent == 1 or pkt.special == 1

ConditionalField(XShortField("chksum",None), checksum_condition)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜