官术网_书友最值得收藏!

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")
)
主站蜘蛛池模板: 铅山县| 郸城县| 千阳县| 河源市| 武宣县| 苏尼特右旗| 清丰县| 新野县| 庐江县| 会宁县| 广平县| 西安市| 信宜市| 辛集市| 宝鸡市| 普兰店市| 宜阳县| 视频| 山东省| 石嘴山市| 吉首市| 绥德县| 庆城县| 云林县| 志丹县| 台中县| 沐川县| 阿拉尔市| 高邮市| 岱山县| 资溪县| 南澳县| 贞丰县| 稻城县| 郸城县| 大同市| 和顺县| 霍州市| 白河县| 馆陶县| 怀柔区|