3

I'm a new member here and also new to python. My question is as follows, is it valid to have a line like this?

if x or y is 'whatever':

I tested this in the interpreter and am getting inconsistent results. It would seem that this line yields more consistent and expected results

if (x or y) is 'whatever':

Or is it always best to explicitly have everything laid out as such

if x is 'whatever' or y is 'whatever':

This last one always works but I'm just trying to make my code a bit more concise while still following best practices. I tried doing a search so as not to ask a redundant question but searching for 'is' 'or' and 'and' is rather difficult. Thanks in advance for any assistance.

edit: Thanks all for the quick replies. This works perfectly for me when I need 'or'

if 'whatever' in [x,y]:

But how would I condense this if I need an 'and'?

if x == 'whatever' and y == 'whatever':
thinc
  • 33
  • 3
  • To your last question, the answer is ``if x==y=='whatever'`` – eyquem Oct 07 '11 at 08:02
  • That means: there's a string object in the memory, whose value is 'whatever', and the object pointed to by the reference whose name is _x_ and the object pointed to by the reference whose name is _y_ are this same object of value 'whatever'. _x_ and _y_ are identifiers ,that is to say strings that are names of variables whose nature is to be pointers, and these pointers point to objects **x** and **y**. The data model of Python is : _x_ identifier, underlying pointer (called reference in Python) of name _x_, the reference points to the object **x**. Think like that and you'll have no problems – eyquem Oct 07 '11 at 08:14

4 Answers4

8

or doesn't work like it does in English.

x or y returns x if x is a true-ish value, otherwise it returns y. Strings are true-ish if they are not empty.

Worse, "is" has a higher precedence that "or", so your expression is the same as x or (y is 'whatever'). So if x is not empty, it returns x (which will be true, so the if will execute). If x is empty, it will evaluate y is 'whatever'.

BTW: Don't use "is" to compare value equality, use ==.

You want this (parens optional):

if (x == 'whatever') or (y == 'whatever'):

or more concise, but stranger:

if 'whatever' in [x, y]:
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
3

Python is not English.

if x or y is 'whatever':

means:

if x or (y is 'whatever'):

Which is X is true OR y is 'whatever'

if (x or y) is 'whatever':

x or y becomes either x or y. If x is true, then it returns X otherwise it becomes Y. Then the result of that is compared to 'whatever'.

You should never never compare strings with is anyways. String should be compared using ==. is means something different which sometimes works by accident.

You actual request can be written as:

if 'whatever' in [x,y]:

This checks whether the string whatever is in the list [x,y].

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
1

I think this would work:

if "whatever" in (x, y):

It's kind of weird though.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
1
if x or y is 'whatever' # this is akin to if x or (y is 'whatever')

if (x or y) is 'whatever': # (x or y) returns the first non-false value...

if x is 'whatever' or y is 'whatever': # this is valid and correct (aside from the fact you shouldn't be using `is`, see below)

You may have

if 'whatever' in (x, y)

or, in case of a longer list of conditions, it's nice to use the 'any' function:

if any([condition(k) for k in list])

But it's overkill in the case you exposed, since you just want to know if 'whatever' is contained in [x, y].

UPDATE:

consider that 'is' is actually comparing the memory addresses, and (as pointed out here) it's not good practice to use it on strings.

Community
  • 1
  • 1
Savino Sguera
  • 3,522
  • 21
  • 20
  • Hmm, I'm not finding the second case to always be false, at least in the interpreter. – thinc Oct 06 '11 at 17:53
  • thinc is correct. Python's version of "x or y" return either one of x,y not an actual boolean value. Also, What's the point of your `any` construction compared to just using in on the whole list? Also, you still say that using is on your third line is valid and correct. (Ok, yes, I'm being a stickler here) – Winston Ewert Oct 06 '11 at 17:59
  • @WinstonEwert I made my point a bit clearer (hopefully!): the question is about condensing conditional statements, so I went a bit more general; you are totally right in the sense that "'whatever' in [x, y]" would be perfectly enough in that case. – Savino Sguera Oct 06 '11 at 19:16