0

I am modifying some Python code that is used in an ODOO Addon.

How does the logic for the following statement work?

origin = (move.group_id and move.group_id.name or (move.origin or move.picking_id.name or "/"))

Values

  • move.group_id is an object
  • move.group_id.name is a string value
  • move.origin is a string value
  • move.picking_id.name is a string

the result is "origin" is assigned the string value of "move.group_id.name"

What is this kind of assignment called?

(I tried to google this first but its not inline if logic and I have a lack of words for what this is called.)

Bertus Kruger
  • 1,345
  • 1
  • 19
  • 31
  • 1
    `(move.origin or move.picking_id.name or "/")` can logically be simplified to just `"/"`... If you do `"any string" or "/"`, the first Truthy value will be returned, `"any string"`. Similar simplfication can be done for `"and"` – OneCricketeer Oct 05 '20 at 19:09
  • 1
    @Carcigenicate For `or` case, there is at least one nonempty string given, so yes. – OneCricketeer Oct 05 '20 at 19:11
  • 1
    That doesn't seem like a great dupe closure. The key is that `and` and `or` evaluate to one of their operands, not only `True`/`False`. Compare `"" or "abc"` and `"z" or "abc"`. – Carcigenicate Oct 05 '20 at 19:11
  • 1
    There's likely a Stack Overflow question that answers this better, but the [docs](https://docs.python.org/3/reference/expressions.html#boolean-operations) describe it. Note the two "The expression . . ." sections. – Carcigenicate Oct 05 '20 at 19:18
  • 1
    "What is this kind of assignment called?" It's just a normal assignment statement. It assigns the value of the expression on the right hand side to the name on the left hand side. it is no different than `x = y + z`. The right hand side is an expression involving [boolean operations](https://docs.python.org/3/reference/expressions.html#boolean-operations). – juanpa.arrivillaga Oct 05 '20 at 19:33

0 Answers0