3

In Scanner data (streaming) I can find the following statement (you can find below the full code):

scanData.updateEvent += onScanData

What does the += mean?

So, I understand that onScanData() is the event handler function that should be called when the scanData.updateEvent is fired.

But why do I have a += here and not a simple =? What get's incremented and where can I find the incremented variable later on?

Remark: to get the code snippet below running, the Interactive-Brokers software TWS has be running.

import datetime
from ib_insync import *

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)


def onScanData(scanData):
    print(scanData[0])
    print(len(scanData))

sub = ScannerSubscription(
    instrument='FUT.US',
    locationCode='FUT.GLOBEX',
    scanCode='TOP_PERC_GAIN')
scanData = ib.reqScannerSubscription(sub)
scanData.updateEvent += onScanData
ib.sleep(60)
ib.cancelScannerSubscription(scanData)
user312087
  • 45
  • 3
  • 1
    `+=` doesn't mean *increment*, it could also mean *concatenate*, *join*, or whatever the object on the left side wants it to mean. – deceze Sep 24 '21 at 14:03
  • 1
    [`Event.__iadd__`](https://github.com/erdewit/eventkit/blob/master/eventkit/event.py#L374) is the same as `Event.connect`. – chepner Sep 24 '21 at 14:06
  • 1
    https://eventkit.readthedocs.io/en/latest/api.html#eventkit.event.Event.connect – deceze Sep 24 '21 at 14:07
  • 1
    The object responsible of firing the event keeps a list of all the event handlers that should be invoked when the event occurs. The += operator in this context indicates adding an event handler to the invocation list. You don't use the = operator because you don't want to cancel previously made subscriptions to the event. – Nir H. Sep 24 '21 at 14:08

1 Answers1

5

reqScannerSubscription returns an instance of ScanDataList. Its updateEvent property is an instance of eventkit.Event. It overloads its __iadd__ method to be an alias for connect, which connects a listener to this event.

Marat
  • 15,215
  • 2
  • 39
  • 48