0

Using a list comprehension I have created a list of 10 different observables. However they always print the same things ("ticker_9") despite their timings being different.

from __future__ import print_function

from rx import Observable
import IPython

n_tickers = 10
tickers = ["ticker_" + str(x) for x in range(n_tickers)]
olist = [Observable.interval(x * 100 + 10).map(lambda _: tickers[x]) for x in range(n_tickers)]
IPython.embed()

So what this does is first, create a list of 10 tickers, named ticker_0 through ticker_9. It then uses a list comprehension to create 10 observables that emit at increasing multiples of 100 milliseconds (plus 10). Each observable (is supposed to) emit a different ticker. However they ALL emit ticker_9 (ie the last ticker in the list), even though the x-variable in the list comprehension is being recognized since they emit at different frequencies.

What's going on here? Why don't I get ticker_0 for olist[0], ticker_1 for olist[1] etc?

Observe:

In [1]: olist[0].subscribe(lambda s: print(s))
Out[1]: <rx.disposables.anonymousdisposable.AnonymousDisposable at 0xb60eaf30>
ticker_9

In [2]: ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9

Or:

In [1]: olist[5].subscribe(lambda s: print(s))
Out[1]: <rx.disposables.anonymousdisposable.AnonymousDisposable at 0xb60c0f30>

In [2]: ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9
ticker_9

... same thing ie. ticker_9 the whole time. Should be ticker_0 and ticker_5, even though in the second case, the emissions are coming in much more slowly.

Why is my list comprehension not working to create a different ticker_x for each observable, even though it is working for the interval?

Thomas Browne
  • 23,824
  • 32
  • 78
  • 121

0 Answers0