Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple repeated `in` keyword [duplicate]

Tags:

python

By mistake I have used more than one in keyword in an expression, but the code still works.

What is the meaning of:

"a" in "bar" in "foo"   # in ... ?

naively I thought that this was equivalent to ("a" in "bar") in "foo" or "a" in ("bar" in "foo") but it is not the case since both are not valid. I get the same behaviour in python2 or 3.

like image 272
Ruggero Turra Avatar asked Jun 29 '17 06:06

Ruggero Turra


People also ask

What are duplicate keywords?

You find yourself with duplicate keywords when you're bidding on the exact same keyword with the exact same match type in a single search campaign more than once. For example, if you're bidding on the exact match keyword [men's blue tennis shoes] twice in one search campaign, you have a duplicate keyword on your hands.

How do I remove a redundant keyword?

If you have a duplicate keyword, pause or remove the one that performs worse. To remove the duplicate keyword, select the checkbox next to the keyword. Then click the Edit drop-down above the table and select Remove.

What is redundant keyword?

Redundant keywords are often confused with non-serving keywords. In reality, redundant keywords are the keywords having the same match type and same ad group.


3 Answers

in is considered a comparison operator, and from Python's documentation for them:

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

like image 52
jamesdlin Avatar answered Nov 23 '22 00:11

jamesdlin


Python evaluates from left to right (you can control the flow / grouping with brackets, tho), and comparison operators can be chained arbitrarily so an expression:

"a" in "bar" in "foo" in "baz" 

Essentially ends up as:

"a" in "bar" and "bar" in "foo" and "foo" in "baz" 

Which resolves to False.

like image 38
zwer Avatar answered Nov 23 '22 00:11

zwer


This seems to mean the following:

("a" in "bar") and ("bar" in "foo") - or False

The following might help:

  • "a" in "bar" in "foo" => False
  • "a" in "bar" in "foobar" => True
  • "b" in "bar" in "foobar" => True
  • "c" in "bar" in "foobar" => False

I thought at first that it might have been "a" in ("bar" in "foo"), but that obviously would return the following:

TypeError: argument of type 'bool' is not iterable

Because ("bar" in "foo") returns False

Edit Fixed obvious typos

like image 25
Rob Gwynn-Jones Avatar answered Nov 23 '22 02:11

Rob Gwynn-Jones