【问题标题】:I am getting "AttributeError: type object 'Observable' has no attribute 'from_'" error in python rx=3.0.1我在 python rx=3.0.1 中收到“AttributeError: type object 'Observable' has no attribute 'from_'”错误
【发布时间】:2019-09-24 18:32:01
【问题描述】:

这是我在笔记本电脑上尝试过的“使用 python 进行反应式编程”一书中的代码。

import sys
from rx import Observable

argv = Observable.from_(sys.argv[1:])
argv.subscribe(
    on_next=lambda i: print("on_next: {}".format(i)),
    on_error=lambda e: print("on_error: {}".format(e)),
    on_completed=lambda: print("on_completed")
)

如果我跑步

python echo1.py hello world there

输出应如下所示:

on_next: hello 
on_next: world 
on_next: there 
on_completed

【问题讨论】:

  • 检查你的 RxPy 版本我遇到了类似的问题,RxPy 3.0.1 中的情况发生了变化

标签: python-3.x rx-py


【解决方案1】:

我相信你的导入是这里的问题,因为 RxPY 3.0.1 导入似乎工作方式不同,你现在直接从 rx 导入 from_

您可能想看看 RxPY 文档中的 Get StartedMigration 部分,他们应该比我现在解释的要好得多。

import sys
from rx import from_

argv = ops.from_(sys.argv[1:])
argv.subscribe(
    on_next=lambda i: print("on_next: {}".format(i)),
    on_error=lambda e: print("on_error: {}".format(e)),
    on_completed=lambda: print("on_completed")
)

【讨论】:

    【解决方案2】:

    现在你不需要 Observable。所有操作都在管道中执行。

    import rx
    from rx import operators as ops
    
    argv = rx.from_(sys.argv[1:])
    
    # argv.map(...).filter(...).subscribe(...) old stuff
    argv.pipe(
        ops.map(...), 
        ops.filter(...)
    ).subscribe(...)
    

    所以忘记 Observable 对象吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      相关资源
      最近更新 更多