如何理解这个相等和赋值语句的逻辑?

时间:2019-07-22 21:43:53

标签: python

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 []

在上面的代码中,如何理解这一行:

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

它的意思是:

if "intent == "how_to_get_started" and user_type == "new" ",这将返回True或False,然后将此布尔值分配给'is_new_user'。是吗?

2 个答案:

答案 0 :(得分:1)

括号在这里:

  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/cloudpickle.py", line 235, in dump
    return Pickler.dump(self, obj)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 409, in dump
    self.save(obj)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 736, in save_tuple
    save(element)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/cloudpickle.py", line 652, in save_instancemethod
    self.save_reduce(types.MethodType, (obj.__func__, obj.__self__), obj=obj)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/cloudpickle.py", line 786, in save_reduce
    save(args)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 736, in save_tuple
    save(element)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 521, in save
    self.save_reduce(obj=obj, *rv)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/cloudpickle.py", line 804, in save_reduce
    save(state)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 821, in save_dict
    self._batch_setitems(obj.items())
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 847, in _batch_setitems
    save(v)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 521, in save
    self.save_reduce(obj=obj, *rv)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/cloudpickle.py", line 804, in save_reduce
    save(state)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 821, in save_dict
    self._batch_setitems(obj.items())
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 847, in _batch_setitems
    save(v)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 781, in save_list
    self._batch_appends(obj)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 808, in _batch_appends
    save(tmp[0])
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 521, in save
    self.save_reduce(obj=obj, *rv)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/cloudpickle.py", line 804, in save_reduce
    save(state)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 821, in save_dict
    self._batch_setitems(obj.items())
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 847, in _batch_setitems
    save(v)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 781, in save_list
    self._batch_appends(obj)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 805, in _batch_appends
    save(x)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 521, in save
    self.save_reduce(obj=obj, *rv)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/cloudpickle.py", line 804, in save_reduce
    save(state)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 821, in save_dict
    self._batch_setitems(obj.items())
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 847, in _batch_setitems
    save(v)
  File "/opt/anaconda3/envs/ds_env_v1.0/lib/python3.6/pickle.py", line 496, in save
    rv = reduce(self.proto)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/py4j-0.10.6-src.zip/py4j/java_gateway.py", line 1160, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/pyspark.zip/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/data/yarn/local/usercache/svc-oozie-workflows/appcache/application_1563419829374_2193/container_e466_1563419829374_2193_01_000001/py4j-0.10.6-src.zip/py4j/protocol.py", line 324, in get_return_value
    format(target_id, ".", name, value))
py4j.protocol.Py4JError: An error occurred while calling o90.__getstate__. Trace:
py4j.Py4JException: Method __getstate__([]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:274)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:745)
exception in __main__
Could not serialize object: Py4JError: An error occurred while calling o90.__getstate__. Trace:
py4j.Py4JException: Method __getstate__([]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:274)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:745)```

如果我们想清楚一点,我们可以将它们分为三个语句:

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

这是基本的布尔代数,但放入python中。 condition1 = (intent == "how_to_get_started") condition2 = (user_type == "new") is_new_user = (condition1 and condition2) 是布尔比较运算符,将返回==True。之后,False的工作原理与您期望的一样:

and

答案 1 :(得分:1)

是的,您是对的!

  

is_new_user =意图==“ how_to_get_started”和user_type ==“新”

赋值在评估最右边的项后从左到右评估。

因此,第一个最右边的部分intent == "how_to_get_started" and user_type == "new" 将被评估,然后从左开始分配,即分配给is_new_user

现在对intent == "how_to_get_started" and user_type == "new"的求值将首先在左边即intent == "how_to_get_started"进行,如果为true,则对右边的部分求值,否则返回false。

访问here了解更多详细信息。

请参见以下示例:-

>>> 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