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

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")
)
主站蜘蛛池模板: 四平市| 抚顺市| 洪洞县| 萨迦县| 兴安盟| 昭平县| 芮城县| 社旗县| 错那县| 石嘴山市| 广平县| 嫩江县| 福清市| 尼勒克县| 乌拉特后旗| 灯塔市| 无锡市| 海林市| 托里县| 克拉玛依市| 洛扎县| 水城县| 大英县| 志丹县| 紫金县| 辽宁省| 保亭| 贵港市| 兴安盟| 达孜县| 曲阜市| 卢湾区| 定边县| 阿巴嘎旗| 绿春县| 南皮县| 平南县| 昂仁县| 宣武区| 高安市| 班戈县|