31

I need to realize a complex if-elif-else statement in Python but I don't get it working.

The elif line I need has to check a variable for this conditions:

80, 443 or 1024-65535 inclusive

I tried

if
  ...
  # several checks
  ...
elif (var1 > 65535) or ((var1 < 1024) and (var1 != 80) and (var1 != 443)):
  # fail
else
  ...
1
  • What doesn't work? That line works perfectly for me for ports 80/81 Commented Mar 22, 2010 at 15:25

7 Answers 7

46

This should do it:

elif var == 80 or var == 443 or 1024 <= var <= 65535:
Sign up to request clarification or add additional context in comments.

Comments

18

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 <= var1 <= 65535)):
  # fail

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 <= p <= 65535: return True
    return False


# ...
elif not PortValidator.port_allowed(var1):
  # fail

7 Comments

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 :)
@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.
@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.
@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.
@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!
|
8
if x == 80 or x == 443 or 1024 <= x <= 65535

should definitely do

1 Comment

But I needed it as elif because I have some more elif-checks before and after this one
4

I think the most pythonic way to do this for me, will be

elif var in [80,443] + range(1024,65535):

although it could take a little time and memory (it's generating numbers from 1024 to 65535). If there's a problem with that, I'll do:

elif 1024 <= var <= 65535 or var in [80,443]:

Comments

2
if
  ...
  # several checks
  ...
elif not (1024<=var<=65535 or var == 80 or var == 443)
  # fail
else
  ...

Comments

1

It is possible to write like this:

elif var1 in [80, 443] or 1024 < var1 < 65535

This way you check if var1 appears in that a list, then you make just 1 check, didn't repeat the "var1" one extra time, and looks clear:

if var1 in [80, 443] or 1024 < var1 < 65535: print 'good' else: print 'bad' ....:
good

Comments

0
if
  ...
  # several checks
  ...
elif ((var1 > 65535) or ((var1 < 1024)) and (var1 != 80) and (var1 != 443)):
  # fail
else
  ...

You missed a parenthesis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.