- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试了解这个 API,但有一个细节让我很困惑。我不习惯使用类和接口(interface),而且我不了解一个细节。
在这个API中有两个非常重要的类:EClient和EWrapper。EClient用于下订单和发送请求。EWrapper 用于处理从 API 接收到的消息。
在所有书籍、网站、教程中...他们创建了一个继承这两个类的新类。我不明白的是 init 函数中使用的代码在不同的来源中是不同的。这怎么可能?
让我说明一下我的意思。
来源 1,他们在 Youtube 上的视频,这是他们如何创建一个继承 EClient 和 EWrapper 类的新类 (TestApp):
https://www.youtube.com/watch?v=dzOilFBDmJI&list=PL71vNXrERKUpPreMb3z1WGx6fOTCzMaH1&index=4
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class TestApp (EClient, EWrapper):
def __init__ (self):
EClient.__init__(self, self)
我有两个问题:
1 为什么他们使用 self 两次? ( self , self )
2 为什么他们不使用 EWrapper 初始化函数?
像这样:
EWrapper.__init__(self, self)
来源 2,一本书“与盈透证券的算法交易(Python 和 C++)”。
这是创建新类的方法:
class SubmitOrder (EClient, EWrapper):
def __init__ (self, addr, port, clientId):
EWrapper.__init__(self)
EClient.__init__(self, self)
我有两个问题:
3 为什么他们在这里调用 EWrapper init 函数?
4 为什么EClient有两个self(self,self),EWrapper有一个self(self)
来源 3,他们自己的文档:
https://interactivebrokers.github.io/tws-api/client_wrapper.html
首先他们说你必须用这段代码实现 EWrapper 接口(interface):
class TestWrapper(wrapper.EWrapper):
但是在 : 之后什么也没有,所以这是我的问题。
5 我应该使用什么代码来实现 EWrapper 接口(interface)?他们谈论接口(interface)而不是类。我读过一个接口(interface)类似于一个类,但定义的方法是抽象的,你应该覆盖这些方法。我知道您不会覆盖 EClient 中的方法,但您必须使用 EWrapper。但我不知道如何“实现 EWrapper 接口(interface)”。不幸的是,他们的代码在每个程序中都应该包含的第一步中看起来并不完整。
在那之后,他们解释说你必须创建一个 EClient 类,你必须在这里使用 EWrapper 才能接收消息:
class TestClient(EClient):
def __init__(self, wrapper):
EClient.__init__(self, wrapper)
这是我的问题:
6 为什么他们使用 wrapper 而不是 EWrapper?据我所知,wrapper 是存储 EWrapper 类的文件。
最后,他们使用 TestWrapper 和 TestClient 来创建新类。这是他们的代码:
class TestApp(TestWrapper, TestClient):
def __init__(self):
TestWrapper.__init__(self)
TestClient.__init__(self, wrapper=self)
这是我的问题:
7 为什么有的初始化函数有一个自己,有的有两个?
我在这里看到 TestClient 中的第二个“self”等于包装器。由于他们的解释,这是有道理的:
“要使用 EClientSocket,首先可能需要将 IBApi.EWrapper 接口(interface)作为其构造函数参数的一部分来实现,以便应用程序可以处理所有返回的消息。从 TWS 发送的消息作为对 IBApi.EClientSocket 中函数调用的响应需要一个 EWrapper 实现,以便可以对其进行处理以满足 API 客户端的需求。”
这表明第二个 self 等于 wrapper,这让我想起了来源 1 中的代码:
EClient.__init__(self, self)
second self 不等于 wrapper,所以这是我的最后一个问题:
8 如果不显式地将 self 等同于 wrapper,这段代码如何工作?
正如您所看到的,我对此非常迷茫,希望有人能解释一下。
感谢您的宝贵时间。
最佳答案
请记住,除了在 SO 上显示 IB 示异常(exception),我不使用 python...我可能错了。
注意,class TestApp (EClient, EWrapper):
表示 TestApp 继承自 Eclient 和 EWrapper。或者,您用于向 TWS 发送消息的客户端和用于接收消息的包装器都是 TestApp
类。
EClient.__init__(self, self)
1 Why are they using self two times? (self, self)
类中的第一个参数始终是 self
来表示它self
,这只是 python 的工作方式。第二个参数是因为 python 需要 EClient init 方法的参数。此参数是数据回调的包装器。
2 Why is that they don't use EWrapper init function?
它什么都不做。如果您查看源代码,它只是一堆要被覆盖的方法。它具有仅记录日志的默认实现。更多我称之为界面的东西。
3 Why is that here they call the EWrapper init function?
也许只是编程风格,它什么都不做。
4 Why is that there are two self (self, self) for EClient and one self (self) for EWrapper.
Ewrapper 不接受任何参数,所以它只是传递在所有 python 类中使用的 self
。
5 What code should I use to implement the EWrapper interface?
有2个类,EClient用于调用TWS\IBG,然后TWS通常会调用IB的服务器,回复会以某种方式返回给您。 TWS 知道将回复发送到哪里的唯一方法是通过在 init 方法中指定包装器来告诉它。
因此,如果您想查看数据,您可以覆盖包装器中的那些数据回调(到目前为止,在这些示例中称为 TestApp 或 SubmitOrder)。这很简单,只需看一下我稍后会展示的一些示例,但基本上只需从 EWrapper 类复制一个方法并将其更改为您想要的。
6 Why do they use wrapper instead of EWrapper?
class TestWrapper(wrapper.EWrapper):
在上面他们会完成 from ibapi import wrapper
所以没有导入 EWrapper,只是一种不同的导入方式。
class TestClient(EClient):
def __init__(self, wrapper):
EClient.__init__(self, wrapper)
在上面的注释中,wrapper 是传递给 init 并重新发送给 EClient init 的参数名称,他们可以随意调用它,但在我看来,重用名称是不好的做法。
- answered in 1.
TestClient.__init__(self, wrapper=self)
上面的注释意味着他们已经创建了 EClient 的 TestClient 子类,并且在 TestApp 中他们使用命名参数 wrapper=self 对其进行初始化。您可以按名称或位置发送参数,因此这里更多地提醒您参数的用途。
The second self is not equal to wrapper, so this is my last question:
8 How can this code work without explicitly equaling self to wrapper?
请记住,在第一段代码中,TestApp 是 EClient 和 EWrapper 的子类,因此 self 确实是包装器。也是客户端。
这是一个不使用多重继承并显示一些重写方法的示例。 https://stackoverflow.com/a/54423878/2855515
关于python - 为什么用不同的__init__代码继承相同的类?盈透证券API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64551093/
我使用的是 PHP 5.3 稳定版,有时会遇到非常不一致的行为。据我所知,在继承中,父类(super class)中的所有属性和方法(私有(private)、公共(public)和 protected
所以我一直在努力寻找正确的方法来让应该非常简单的继承发挥作用(以我想要的方式 ;)),但我失败得很惨。考虑一下: class Parent { public String name = "Pare
给定这些类: class Father { public Father getMe() { return this; } } class Child extends Father {
为什么最后打印“I'm a Child Class”。 ? public class Parent { String parentString; public Parent()
我知道有很多类似的问题对此有很多很好的答案。我试着看看经典的继承方法,或者那些闭包方法等。不知何故,我认为它们对我来说或多或少是“hack”方法,因为它并不是 javascript 设计的真正目的。
我已经使用表单继承有一段时间了,但没有对以下方法进行太多研究。只需创建一个新类而不是表单并从现有表单继承并根据需要将所需控件转换为 protected 。 Visual Studio 2010 设计器
我原以为下面的代码片段会产生编译错误,因为派生类不会有我试图在 pub_fun() 中访问的 priv_var。但是它编译了,我得到了下面提到的输出。有人可以解释这背后的理论吗? class base
继承的替代方案有哪些? 最佳答案 Effective Java:优先考虑组合而不是继承。 (这实际上也来自《四人帮》)。 他提出的情况是,如果扩展类没有明确设计为继承,继承可能会导致许多不恰当的副作用
我有2个类别:动物( parent )和狗(动物的“ child ”),当我创建一个 Animal 对象并尝试提醒该动物的名称时,我得到了 undefined ,而不是她的真名。为什么?(抱歉重复发帖
我试图做继承,但没想到this.array会像静态成员一样。我怎样才能让它成为“ protected /公开的”: function A() { this.array = []; } func
在创建在父类中使用的 lambda 时,我试图访问子类方法和字段。代码更容易解释: class Parent { List> processors; private void do
如果我有一个对象,我想从“ super 对象”“继承”方法以确保一致性。它们将是混合变量。 修订 ParentObj = function() { var self = this; t
class Base { int x=1; void show() { System.out.println(x); } } class Chi
目前我正在尝试几种不同的 Javascript 继承方法。我有以下代码: (“借用”自 http://www.kevlindev.com/tutorials/javascript/inheritanc
我在 .popin-foto 元素中打开一个 popin。当我尝试在同一元素中打开子类 popin 时,它不起作用。 代码 这是 parent function Popin(container, ti
我有以下两个类: class MyClass { friend ostream& operatorvalue +=1; return *this; } 现在
有没有办法完全忽略导入到 html 文件中的 header 中的 CSS 文件? 我希望一个页面拥有自己独立的 CSS,而不是从任何其他 CSS 源继承。 最佳答案 您可以在本地样式表中使用 !imp
Douglas Crockford似乎喜欢下面的继承方式: if (typeof Object.create !== 'function') { Object.create = functio
假设我有以下代码: interface ISomeInterface { void DoSomething(); void A(); void B(); } public
class LinkedList{ public: int data; LinkedList *next; }; class NewLinkedList: public Lin
我是一名优秀的程序员,十分优秀!