0

What is the pythonic way to write the condition that checks that both variables cannot be none, and both variables also cannot be not None. For example

if a is None and b is None: raise SystemExit(1)
if a is not None and b is not None: raise SystemExit(1)
# rest of the code
yayu
  • 7,758
  • 17
  • 54
  • 86
  • Looks pythonic enough to me. What exactly is the problem here? – Martijn Pieters Nov 03 '14 at 12:27
  • 2
    Are you using `pass` here because you have an `else` statement? In that case, why not use `if a is None or b is None:` for the opposite test? – Martijn Pieters Nov 03 '14 at 12:28
  • @MartijnPieters If both `a` and `b` are None then I will raise an exception that this class needs atleast one of these arguments to run. – yayu Nov 03 '14 at 12:29
  • Then you want to check for `if a is None and b is None`, right? – RemcoGerlich Nov 03 '14 at 12:30
  • 1
    @yayu: then your test is not correct. You'd use `if a is None and b is None: raise Exception`. – Martijn Pieters Nov 03 '14 at 12:31
  • @MartijnPieters you're right checking if None is better. I am actually performing an xor test... only one among `a` or `b` can be `None` to pass, otherwise if both are None, or both are not None, it raises an exception. In the original problem I just wrote pass as a placeholder to keep the details out. – yayu Nov 03 '14 at 12:32
  • 2
    @yayu: then *make that part of your question*. As it stands it is just too vague to answer in any meaningful way and risks being closed as 'unclear', 'too broad' or 'primarily opinion based'. – Martijn Pieters Nov 03 '14 at 12:34
  • 1
    This is now a duplicate of [How do you get the logical xor of two variables in Python?](http://stackoverflow.com/q/432842); you can use `bool(a is None) != bool(b is None)` or `bool(a is None) ^ bool(b is None)`. – Martijn Pieters Nov 03 '14 at 12:37
  • 2
    possible duplicate of [How do you get the logical xor of two variables in Python?](http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python) – Rob Grant Nov 03 '14 at 12:38
  • Both variables cannot be none means one of them holds some value so `if (a or b)` – Irshad Bhat Nov 03 '14 at 12:39
  • 1
    @BHATIRSHAD: `a = 0` is also a valid value, but `bool(a)` is `False`. An explicit test for `None` can have specific meaning you are ignoring. – Martijn Pieters Nov 03 '14 at 12:41
  • You can use `if a : pass` – d-coder Nov 03 '14 at 12:48
  • @d-coder: no you cannot. `a = 0` is not `None` but false in a boolean context. – Martijn Pieters Nov 03 '14 at 12:51
  • @MartijnPieters `for a in None,0:if a :print "not empty"` would print the statement ? – d-coder Nov 03 '14 at 12:55
  • why i this on hold? it is a stupid question, yes .. and a duplicate. but not unclear in any form. –  Nov 03 '14 at 13:12

2 Answers2

1
if (a is None) != (b is None):
    raise SystemExit("kkthxbye")
# remainder of code

What's the difference between XOR and NOT-EQUAL-TO?

Community
  • 1
  • 1
-1

You can try if all((a,b is not None)): pass

In [31]: a = 1

In [32]: b = 1

In [33]: all((a,b is not None))
Out[33]: True

In [34]: b = None

In [35]: all((a,b is not None))
Out[35]: False

Note: Setting either of value of a or b to 0 will yield True

Pythonic?

In [36]: len('if a is not None and b is not None')
Out[36]: 34

In [37]: len('all((a,b is not None))')
Out[37]: 22
Yax
  • 2,127
  • 5
  • 27
  • 53