所以这是关于我所假设的两个问题,就是我的基本混淆.我希望没关系.
这里有一些代码:
import numpy as np
class new_array(np.ndarray):
def __new__(cls, array, foo):
obj = array.view(cls)
obj.foo = foo
return obj
def __array_finalize__(self, obj):
print "__array_finalize"
if obj is None: return
self.foo = getattr(obj, 'foo', None)
def __getitem__(self, key):
print "__getitem__"
print "key is %s"%repr(key)
print "self.foo is %d, self.view(np.ndarray) is %s"%(
self.foo,
repr(self.view(np.ndarray))
)
self.foo += 1
return super(new_array, self).__getitem__(key)
print "Block 1"
print "Object construction calls"
base_array = np.arange(20).reshape(4,5)
print "base_array is %s"%repr(base_array)
p = new_array(base_array, 0)
print "\n\n" …Run Code Online (Sandbox Code Playgroud) 我正在努力子类化我自己的numpy.ndarray子类.我真的不明白问题是什么,并希望有人解释在下列情况下出了什么问题,以及如何做我正在尝试做的事情.
我有一个numpy.ndarry的子类,其行为符合我的要求(下面的代码中的A类).我想子类A(下面的代码中的B类),以便B包含其他信息(名称)和方法(装饰的.simple_data方法).
import numpy as np
class A(np.ndarray):
def __new__(cls,data):
obj = np.asarray(data).view(cls)
return obj
def __array_finalize(self,obj):
if obj is None: return
class B(A):
def __init__(self,data,name):
super(B,self).__init__(data)
self.name = name
@property
def simple_data(self):
return [data[0,:],data[:,0]]
if __name__ == '__main__':
data = np.arange(20).reshape((4,5))
b = B(data,'B')
print type(b)
print b.simple_data
Run Code Online (Sandbox Code Playgroud)
运行此代码会生成输出:
Traceback (most recent call last):
File "ndsubclass.py", line 24, in <module>
b = B(data,'B')
TypeError: __new__() takes exactly 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud)
我假设这与B的构造中的'name'变量有关,并且由于A是numpy.array的子类,因此在B的init方法之前调用A的新方法.因此,为了解决这个问题,我假设B还需要一个适当处理附加参数的新 …
交互式unison命令行提供以下帮助:
Commands:
<ret> or f or <spc> follow unison's recommendation (if any)
n or j go to the next item
p or b or k go back to previous item
<del> or <bsp> revert then go back to previous item
0 go to the start of the list
9 go to the end of the list
5 go forward to the middle of the following items
6 go backward to the middle of the preceding items
R reverse …Run Code Online (Sandbox Code Playgroud) 问题是,对具有多重继承的类的第二个超类的nullptr的隐式转换(至少在LLVM 7.0.2中)在调整中应用于nullptr.指针现在不再为null(如果在超类的方法中执行空检查)可能导致崩溃(我猜技术上是未定义的行为).
这是一个最小的例子:
#include <iostream>
inline bool pointerIsNotNull(const void* ptr) { return ptr != nullptr; }
class IntValue {
public:
IntValue() { }
int getIntValue() { return pointerIsNotNull(this) ? value : 0; }
private:
int value;
};
static const char* nullptrChar = "nullptr";
class CharValue {
public:
CharValue() { }
const char* getCharValue() { return pointerIsNotNull(this) ? value : nullptrChar; }
private:
char* value;
};
class Foo : public IntValue, public CharValue {
public:
Foo() { }
double getDoubleValue() { …Run Code Online (Sandbox Code Playgroud)