Avoiding backslashes

The last small thing that prevents RxPY code from being as readable as synchronous procedural code is the usage of backslashes for continuation lines. When an operator chain is quite small, like in the previous examples, then using backslashes is natural. However, it becomes a visual distraction when longer operator chains are used. The easy way to fix this is to wrap expressions in parentheses. Since line continuation is implicit inside an open parenthesis, there is no need to add backslashes.

Moreover, comments are allowed inside the parentheses. This allows us to finally write the code snippet this way:

from collections import namedtuple

Basket = namedtuple('Basket', ['count', 'what'])
FruitCount = namedtuple('FruitCount', ['count', 'double'])

obs = (
Observable.from_([Basket(count=5, what='apple'),
Basket(count=3, what='orange')])
.filter(lambda i: i.what == 'apple')
# Now I count apples
.map(lambda i: i.count)
.map(lamda i: FruitCount(count=i, double=i*2))
)
...

These small changes, when applied in many functions with longer operator chains, really make the difference between easily following the code flow and being distracted by some syntactic overhead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.80.101