erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.75k stars 723 forks source link

class IB: vs class IB(Wrapper, Client) #3

Closed jaredvacanti closed 6 years ago

jaredvacanti commented 6 years ago

Is there a functional reason why the switch was made from sub-classing directly to storing in attributes?

ib_insync:

class IB:

    def __init__(self):
        self.wrapper = Wrapper()
        self.client = Client(self.wrapper)

vs the standard ibapi:

class IB(Wrapper, Client)

    def __init__(self):
        Wrapper.__init__(self)
        Client.__init__(self, wrapper=self)

Is there a reason that depends on the asyncio integration?

erdewit commented 6 years ago

It gives a cleaner design, with the IB class containing no state of its own and just coordinating between client and wrapper. Otherwise it would have been one giant class with everything in it.

Also, directly inheriting from Wrapper and Client pulls in all the request and wrapper methods (around 150?). All of them useless to the user of the IB class. And what is worse, the method names would clash with what I want to use for IB. For example accountSummary is a wrapper method but I want it to return a list of account values.

So composition is used to keep a clean namespace and the freedom to choose any method name.