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

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")
)
主站蜘蛛池模板: 建德市| 阜阳市| 孟州市| 清涧县| 博乐市| 诸城市| 运城市| 长沙市| 罗城| 柳州市| 石泉县| 盱眙县| 梅州市| 苗栗县| 盖州市| 南召县| 渝中区| 灯塔市| 龙口市| 福贡县| 洛浦县| 肃北| 四平市| 阜新市| 北京市| 专栏| 牙克石市| 新野县| 台中市| 昌平区| 江油市| 奉新县| 五台县| 大理市| 新蔡县| 庆安县| 麻江县| 江口县| 安达市| 天等县| 鹤山市|