Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Please feel free to review it based on best practices, style and any other efficient solution.

@mjolka @mjolka already provided a more natural alternative implementation I completely agree with: following the logic precisely as in the problem statement is natural and easy to understand. I think the reason why it's easier to understand than yours has to do with the fact that the ranges being evaluated in the 2 distinct cases depending on the value of outside_mode have an overlap on the values 1 and 10. If the two ranges were complements of each other, things would be simpler.

As for style:

if(n>1 and n<10):
 return not outside_mode
else:
 return outside_mode or n is 10 or n is 1

The parentheses in the if condition are redundant. And PEP8 recommends to put spaces around operators, so this would be better:

if n > 1 and n < 10:

Even better is to use Python's kick-ass ... < ... < ... operator:

if 1 < n < 10:

Be careful with using is. In this example it works as expected, but if you try to use for non-primitive types, it might not give what you expect:

>>> [1, 2] is [1, 2]
False
>>> (1, 2) is (1, 2)
False

Unless there is a specific reason to use is, I think you should use ==:

return outside_mode or n == 10 or n == 1

Please feel free to review it based on best practices, style and any other efficient solution.

@mjolka already provided a more natural alternative implementation I completely agree with: following the logic precisely as in the problem statement is natural and easy to understand. I think the reason why it's easier to understand than yours has to do with the fact that the ranges being evaluated in the 2 distinct cases depending on the value of outside_mode have an overlap on the values 1 and 10. If the two ranges were complements of each other, things would be simpler.

As for style:

if(n>1 and n<10):
 return not outside_mode
else:
 return outside_mode or n is 10 or n is 1

The parentheses in the if condition are redundant. And PEP8 recommends to put spaces around operators, so this would be better:

if n > 1 and n < 10:

Even better is to use Python's kick-ass ... < ... < ... operator:

if 1 < n < 10:

Be careful with using is. In this example it works as expected, but if you try to use for non-primitive types, it might not give what you expect:

>>> [1, 2] is [1, 2]
False
>>> (1, 2) is (1, 2)
False

Unless there is a specific reason to use is, I think you should use ==:

return outside_mode or n == 10 or n == 1

Please feel free to review it based on best practices, style and any other efficient solution.

@mjolka already provided a more natural alternative implementation I completely agree with: following the logic precisely as in the problem statement is natural and easy to understand. I think the reason why it's easier to understand than yours has to do with the fact that the ranges being evaluated in the 2 distinct cases depending on the value of outside_mode have an overlap on the values 1 and 10. If the two ranges were complements of each other, things would be simpler.

As for style:

if(n>1 and n<10):
 return not outside_mode
else:
 return outside_mode or n is 10 or n is 1

The parentheses in the if condition are redundant. And PEP8 recommends to put spaces around operators, so this would be better:

if n > 1 and n < 10:

Even better is to use Python's kick-ass ... < ... < ... operator:

if 1 < n < 10:

Be careful with using is. In this example it works as expected, but if you try to use for non-primitive types, it might not give what you expect:

>>> [1, 2] is [1, 2]
False
>>> (1, 2) is (1, 2)
False

Unless there is a specific reason to use is, I think you should use ==:

return outside_mode or n == 10 or n == 1
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

Please feel free to review it based on best practices, style and any other efficient solution.

@mjolka already provided a more natural alternative implementation I completely agree with: following the logic precisely as in the problem statement is natural and easy to understand. I think the reason why it's easier to understand than yours has to do with the fact that the ranges being evaluated in the 2 distinct cases depending on the value of outside_mode have an overlap on the values 1 and 10. If the two ranges were complements of each other, things would be simpler.

As for style:

if(n>1 and n<10):
 return not outside_mode
else:
 return outside_mode or n is 10 or n is 1

The parentheses in the if condition are redundant. And PEP8 recommends to put spaces around operators, so this would be better:

if n > 1 and n < 10:

Even better is to use Python's kick-ass ... < ... < ... operator:

if 1 < n < 10:

Be careful with using is. In this example it works as expected, but if you try to use for non-primitive types, it might not give what you expect:

>>> [1, 2] is [1, 2]
False
>>> (1, 2) is (1, 2)
False

Unless there is a specific reason to use is, I think you should use ==:

return outside_mode or n == 10 or n == 1
lang-py

AltStyle によって変換されたページ (->オリジナル) /