- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想首先说我知道new.instancemethod
Python 3 中不存在重载函数,从技术上讲也不存在重载函数。
但是,我正在查看一个旧的 python 包,它使用 new.instancemethod
在有效允许 overloaded functions
的脚本中。
具体包可以从 this site 找到尽管我已经包含了我认为是以下相关部分的内容。
我正在使用 python 3,因此我需要将其转换为 python 3 的可用脚本。我已经使用了 2to3 工具并已经查看了大部分更改。但是,我被困在下面,我不知道要替换什么 new.instancemethod
和。
class overloaded:
...
def __get__(self, obj, type=None):
if obj is None:
return self
return new.instancemethod(self, obj)
__init__
前面使用了类重载作为装饰器,不会造成任何问题,但是当它在方法前面使用时就会出现问题,因为 obj
不再是None
.
我需要这个的原因是,a) 它很有用,b) 因为我目前正在将过时的 IbPy 包从 2.56 翻译到 3.x。 IbPy 包基本上是将 Interactive Brokers 的 Java API 翻译成 Python。
@overloaded前面的main方法如下:
@overloaded
@synchronized(mlock)
def eConnect(self, host, port, clientId):
host = self.checkConnected(host)
if host is None:
return
try:
socket = Socket(host, port)
self.eConnect(socket, clientId)
except Exception as e:
self.eDisconnect()
self.connectionError()
请注意,@synchronize 只是一个强制多个函数同步的装饰器。代码很短,可以找到here ,尽管我认为这与我的问题无关。
经过这么长的解释,我如何得到 return new.instancemethod(self, obj)
想了解更多 python 3 吗?尽管我昨晚和今天早上都读了相关内容,但我似乎无法理解正在发生的事情。我已经尝试了很多事情,我想我只是缺乏对 python 如何处理这个问题的理解。
如有任何建议,我们将不胜感激。谢谢!
最佳答案
应该使用
types.MethodType
而不是 new.instancemethod
或者您可以从任何类获取实例方法:
instancemethod = type(Anyclass.method)
其中 method 是任何类方法
关于python - new.instancemethod 在重载脚本中转换为 python 3.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266949/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!