- Hands-On Reactive Programming with Python
- Romain Picard
- 168字
- 2021-06-24 18:25:23
The of operator
The of operator is a variant of the from_ operator. This operator creates an observable from a variable number of arguments. The marble diagram of this operator is shown in the following diagram:
Figure 4.1: The of operator
Its prototype is as follows:
Observable.of(*args, **kwargs)
The scheduler can be provided as the optional scheduler keyword argument. This operator can be used with values directly passed to it, as follows:
numbers = Observable.of(1, 2, 3, 4)
numbers.subscribe(
on_next=lambda i: print("item: {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)
Upon subscription, the sequence provided as the argument of the of operator is emitted on the observable before it completes, as follows:
item: 1 item: 2 item: 3 item: 4 completed
The of operator can also be used to create an observable from the arguments of a function. The following example returns the same values as the previous one:
def create_numbers_observable(*args):
return Observable.of(*args)
create_numbers_observable(1, 2, 3, 4).subscribe(
on_next=lambda i: print("item: {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)
推薦閱讀
- 30天自制操作系統(tǒng)
- Linux設(shè)備驅(qū)動(dòng)開(kāi)發(fā)詳解(第2版)
- Ubuntu Linux操作系統(tǒng)
- 開(kāi)源安全運(yùn)維平臺(tái)OSSIM疑難解析:入門(mén)篇
- 高性能Linux服務(wù)器構(gòu)建實(shí)戰(zhàn):運(yùn)維監(jiān)控、性能調(diào)優(yōu)與集群應(yīng)用
- Linux網(wǎng)絡(luò)內(nèi)核分析與開(kāi)發(fā)
- 計(jì)算機(jī)系統(tǒng)開(kāi)發(fā)與優(yōu)化實(shí)戰(zhàn)
- 直播系統(tǒng)開(kāi)發(fā):基于Nginx與Nginx-rtmp-module
- NetDevOps入門(mén)與實(shí)踐
- 計(jì)算機(jī)系統(tǒng)的自主設(shè)計(jì)
- Windows 8實(shí)戰(zhàn)從入門(mén)到精通(超值版)
- Python UNIX和Linux系統(tǒng)管理指南
- 分布式系統(tǒng)設(shè)計(jì)實(shí)踐
- Linux內(nèi)核API完全參考手冊(cè)(第2版)
- 完美應(yīng)用Ubuntu(第2版)