It's often easier to think in the positive sense, and wrap it in a not:
elif not (var1 == 80 or var1 == 443 or (1024
You could of course also go all out and be a bit more object-oriented:
class PortValidator(object): @staticmethod def port_allowed(p): if p == 80: return True if p == 443: return True if 1024
answered Mar 22, 2010 at 15:23
398k 64 64 gold badges 482 482 silver badges 615 615 bronze badges
Why make it part of a class? A global method would be more readable, and is more natural unless you're from a Java background :)
Commented Mar 22, 2010 at 15:28@extraneon: Mainly to get the word "PortValidator" in there somehow, as a context for the method. Might not be 100% proper Pythonic code, but that was my thinking.
Commented Mar 22, 2010 at 15:36@Neverland Easily readable tends to be much more important than the amount of code. Code you can read saves you both time and bugs, and unless it's very performance critical code you should be wary of difficult to read/understand structures.
Commented Mar 22, 2010 at 15:39@mizipor building a class just to put a staticmethod in is not really OO in my view:) A nonstatic method would IMHO be better so it could get configuration methods transparantly.
Commented Mar 22, 2010 at 15:41@Khelben: I think most of us are too used to writing code in C, where 1 < a < 5 gets converted to (1 < a) < 5 which is always true, since (1 < a) is a bool with value of 0 or 1, and is always less than 5. It's nice that Python provides more human-readable range syntax!