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

The from_ operator

The from_ operator is the operator that was used in the previous example to create an observable from a list. The marble diagram of this operator is shown in the following figure:

Figure 1.11: The from_ operator
The name of this operator is strange because it ends with an underscore. The reason for this underscore is that from is a reserved keyword in Python, so it was not possible to name this operator as it should be; that is,  from. If you dislike this notation, there is an alias named from_list. You can use it for a more Pythonic code at the expense of a longer name.

The prototype of this operator is the following one:

Observable.from_(iterable, scheduler=None)

The first parameter accepts any iterable object. This includes lists, tuples, dictionaries, and any class that implements the iterator methods __iter__ and next. The second parameter is used to provide a scheduler that will be used to emit the items of the observable. This parameter is present on all creation operators. It is useful when running in an asynchronous environment or an environment with concurrency. We will study schedulers in detail in Chapter 5Concurrency and Paralellism in RxPY.

The from_ operator creates an observable that emits one item per entry in the iterable. It then completes the observable. Here are some examples of usage which show the items that they return:

Observable.from_(sys.argv) # argv[0], argv[1], argv[2]..., completed
Observable.from_([1, 2, 3, 4]) # 1, 2, 3, 4, completed
Observable.from_({'foo': 'fooz', 'bar': 'barz'}) # 'foo', 'bar', completed

Note that when using a dictionary, the observable contains the keys of the dictionary and not the values. This is the same behavior as a classic Python iteration on a dictionary using a for loop.

主站蜘蛛池模板: 和龙市| 宝兴县| 衡山县| 宁阳县| 吉木萨尔县| 台东市| 英超| 龙井市| 友谊县| 肇东市| 安达市| 洪江市| 鞍山市| 奇台县| 乌拉特后旗| 辽宁省| 辽源市| 洪洞县| 天峨县| 临湘市| 阳曲县| 武山县| 尤溪县| 贵阳市| 平安县| 固安县| 宁都县| 咸丰县| 监利县| 吴旗县| 富裕县| 宝丰县| 陇南市| 千阳县| 图木舒克市| 获嘉县| 扶风县| 镇原县| 兴宁市| 武陟县| 台北县|