-1
intent = tracker.latest_message["intent"].get("name")
user_type = next(tracker.get_latest_entity_values("user_type"), None)
is_new_user = intent == "how_to_get_started" and user_type == "new" 
if intent == "affirm" or is_new_user:
    return [SlotSet("onboarding", True)]
elif intent == "deny":
    return [SlotSet("onboarding", False)]

return []

In the code above, how to understand this line:

is_new_user = intent == "how_to_get_started" and user_type == "new" 

Does it mean:

if "intent == "how_to_get_started" and user_type == "new" ", this will return a True or False, then assign this bool to 'is_new_user'. Is that right?

bharatk
  • 4,202
  • 5
  • 16
  • 30
marlon
  • 99
  • 1
  • 8
  • 2
    well what happens when you try it in a python console? – SuperStew Jul 22 '19 at 21:45
  • 1
    Possible duplicate of [Strange use of "and" / "or" operator](https://stackoverflow.com/questions/47007680/strange-use-of-and-or-operator) – pault Jul 22 '19 at 21:47

2 Answers2

1

Here's where the parentheses go:

is_new_user = ((intent == "how_to_get_started") and (user_type == "new"))

We could instead break these into three statements if we wanted to be verbosely clear:

condition1 = (intent == "how_to_get_started")
condition2 = (user_type == "new")
is_new_user = (condition1 and condition2)

This is basic boolean algebra, but put into python. == is the boolean comparison operator, and will return either True or False. After which, and works exactly like you'd expect:

| condition1 | condition2 | is_new_user |
| ---------- | ---------- | ----------- |
| True       | True       | True        |
| True       | False      | False       |
| False      | True       | False       |
| False      | False      | False       |
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
1

YES YOU'RE RIGHT !

is_new_user = intent == "how_to_get_started" and user_type == "new"

The assignment evaluates from left to right after evaluating the rightmost term.

Thus firstly rightmost part intent == "how_to_get_started" and user_type == "new" will be evaluated, which is then assigned from left, i.e, to is_new_user.

Now evaluation of intent == "how_to_get_started" and user_type == "new", will occur firstly on left i.e, intent == "how_to_get_started" and if that is true, then the right part is evaluated, else returned false.

Visit here for more details.

See this example:-

>>> import dis
>>> def foo(): var = 36 == 6*6 and 4 == 3*2
... 
>>> dis.dis(foo)
  1           0 LOAD_CONST               1 (36)
              2 LOAD_CONST               6 (36)
              4 COMPARE_OP               2 (==)
              6 JUMP_IF_FALSE_OR_POP    14
              8 LOAD_CONST               3 (4)
             10 LOAD_CONST               7 (6)
             12 COMPARE_OP               2 (==)
        >>   14 STORE_FAST               0 (var)
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE
Sidhant Rajora
  • 307
  • 4
  • 16
Vicrobot
  • 3,795
  • 1
  • 17
  • 31