这是我在几种语言中错过的一个功能,想知道是否有人知道如何在 Python 中完成它。
这个想法是我有一个基类:
class Base(object):
def __init__(self):
self.my_data = 0
def my_rebind_function(self):
pass
Run Code Online (Sandbox Code Playgroud)
和一个派生类:
class Child(Base):
def __init__(self):
super().__init__(self)
# Do some stuff here
self.my_rebind_function() # <==== This is the line I want to get rid of
def my_rebind_function(self):
# Do stuff with self.my_data
Run Code Online (Sandbox Code Playgroud)
从上面可以看出,我有,我想叫反弹功能后的Child.__init__已完成其工作。而且我希望对所有继承的类都这样做,所以如果它由基类执行会很棒,所以我不必在每个子类中重新键入该行。
如果该语言具有__finally__类似的功能,其操作方式类似于它在异常情况下的操作方式,那就太好了。也就是说,它应该在所有__init__(所有派生类的)函数都运行后运行,那会很棒。所以调用顺序将是这样的:
Base1.__init__()
...
BaseN.__init__()
LeafChild.__init__()
LeafChild.__finally__()
BaseN.__finally__()
...
Base1.__finally__()
Run Code Online (Sandbox Code Playgroud)
然后对象构建完成。这也有点类似于使用setup,run和teardown函数进行单元测试。
我有以下代码:
typedef float vec3_t[3];
void f(const vec3_t v[2]){
// do stuff
}
int main(int argc, char * argv[]){
vec3_t v[2];
v[2][1] = 1;
f(v);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不会使用编译
gcc main.c -std=gnu99 -O0 -o main
Run Code Online (Sandbox Code Playgroud)
但是给出错误
main.c: In function ‘main’:'
main.c:293:5: warning: passing argument 1 of ‘f’ from incompatible pointer type [enabled by default]
f(v);
^
main.c:286:6: note: expected ‘const float (*)[3]’ but argument is of type ‘float (*)[3]’
void f(const vec3_t v[2]){
^
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我删除函数f中的const要求.这一切都运作良好.我无法弄清楚出了什么问题?