我有一个AbstractDataHandle类,它的init方法和类Classifier.我想在Classifier中有两个构造函数,Java就像.一个继承自它的超类,一个全新的.
它会像(但我打算"保留"两个构造函数):
class AbstractDataHandle():
def __init__(self, elements, attributes, labels):
self._load(elements, attributes, labels)
class Classifier(AbstractDataHandle):
def __init__(self, classifier="LinearSVC", proba=False):
self._fit(classifier, proba)
Run Code Online (Sandbox Code Playgroud)
我可以在一个班级中拥有两个构造函数吗?如果是,我可以从超类继承构造函数,并添加一个新的构造函数吗?
先感谢您.
我有一个类层次结构A < - B < - C,在B中,我需要在构造函数中进行一些处理,所以我从这篇文章中想出了这个代码:用__init __()方法理解Python super()
#!/usr/bin/python
class A(object):
def __init__(self, v, v2):
self.v = v
self.v2 = v2
class B(A):
def __init__(self, v, v2):
# Do some processing
super(self.__class__, self).__init__(v, v2)
class C(B):
def hello():
print v, v2
b = B(3, 5)
print b.v
print b.v2
c = C(1,2)
print c
Run Code Online (Sandbox Code Playgroud)
但是,我遇到超出最大递归的运行时错误
File "evenmore.py", line 12, in __init__
super(self.__class__, self).__init__(v, v2)
RuntimeError: maximum recursion depth exceeded while calling a Python object
Run Code Online (Sandbox Code Playgroud)
可能有什么问题?
从这样的两个对象继承时
class Foo(object):
def __init__(self,a):
self.a=a
class Bar(object):
def __init__(self,b):
self.b=b
Run Code Online (Sandbox Code Playgroud)
我通常会这样做
class FooBar(Foo,Bar):
def __init__(self,a,b):
Foo.__init__(self,a)
Bar.__init__(self,b)
Run Code Online (Sandbox Code Playgroud)
超级怎么知道我是否想要同时打电话?如果是这样,它将如何知道哪个参数传递到哪里.或者根本不可能在这里使用超级?
即使Foo和Bar采取相同的论点可以super处理这个?
或者我不应该首先尝试这样做吗?
我看了一下,但我无法找到错误的答案.这是代码:
import tkinter as tk
root=tk.Tk()
class Page(tk.Frame):
'''Enables switching between pages of a window.'''
def __init__(self):
self.widgets={}
self.grid(column=0,row=0)
page=Page()
tk.mainloop()
Run Code Online (Sandbox Code Playgroud)
这是错误:
Traceback (most recent call last):
File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line 11, in <module>
page=Page()
File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line , in __init__
self.grid(column=0,row=0)
File "C:\Python34\lib\tkinter\__init__.py", line 2055, in grid_configure
self.tk.call(
AttributeError: 'Page' object has no attribute 'tk'
Run Code Online (Sandbox Code Playgroud)
我对tkinter很新,这个错误令我难过.我非常感谢任何帮助,谢谢!
我有以下两个超类:
class Parent1(object):
def on_start(self):
print('do something')
class Parent2(object):
def on_start(self):
print('do something else')
Run Code Online (Sandbox Code Playgroud)
我希望有一个继承的子类可以为父母双方打电话.
class Child(Parent1, Parent2):
def on_start(self):
# super call on both parents
Run Code Online (Sandbox Code Playgroud)
什么是Pythonic的方法呢?谢谢.
这是super()的合法使用吗?
class A(object):
def method(self, arg):
pass
class B(A):
def method(self, arg):
super(B,self).method(arg)
class C(B):
def method(self, arg):
super(B,self).method(arg)
Run Code Online (Sandbox Code Playgroud)
谢谢.
我的代码:
class AError(Exception):
print 'error occur'
for i in range(3):
try:
print '---oo'
raise AError
except AError:
print 'get AError'
else:
print 'going on'
finally:
print 'finally'
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,输出是这样的:
error occur
---oo
get AError
finally
---oo
get AError
finally
---oo
get AError
finally
Run Code Online (Sandbox Code Playgroud)
我认为字符串"error occur"应该出现三次"---oo",但它只发生一次; 为什么?
在ctypes结构中,是否可以指定默认值?
例如,使用常规python函数,您可以这样做:
def func(a, b=2):
print a + b
Run Code Online (Sandbox Code Playgroud)
这将允许这种行为:
func(1) # prints 3
func(1, 20) # prints 21
func(1, b=50) # prints 51
Run Code Online (Sandbox Code Playgroud)
是否可以在ctypes结构中执行此操作?
例如:
class Struct(Structure):
_fields_ = [("a", c_int), ("b", c_int)] # b default should be 2
def print_values(self):
print self.a, self.b
struct_instance = Struct(1)
struct_instance.print_values() # should somehow print 1, 2
Run Code Online (Sandbox Code Playgroud) 我目前有一个叫做Polynomial的类,初始化看起来像这样:
def __init__(self, *termpairs):
self.termdict = dict(termpairs)
Run Code Online (Sandbox Code Playgroud)
我通过使键成为指数并且相关的值是系数来创建多项式.要创建此类的实例,请输入如下内容:
d1 = Polynomial((5,1), (3,-4), (2,10))
Run Code Online (Sandbox Code Playgroud)
这使得字典如此:
{2: 10, 3: -4, 5: 1}
Run Code Online (Sandbox Code Playgroud)
现在,我想创建一个名为Quadratic的Polynomial类的子类.我想在Quadratic类构造函数中调用Polynomial类构造函数,但是我不太清楚如何做到这一点.我试过的是:
class Quadratic(Polynomial):
def __init__(self, quadratic, linear, constant):
Polynomial.__init__(self, quadratic[2], linear[1], constant[0])
Run Code Online (Sandbox Code Playgroud)
但我得到错误,任何人都有任何提示?当我调用Polynomial类构造函数时,我觉得我使用了不正确的参数.
我有一节课:
class A(object):
def __init__(self, *args):
# impl
Run Code Online (Sandbox Code Playgroud)
也是一个"mixin",基本上是另一个带有一些数据和方法的类:
class Mixin(object):
def __init__(self):
self.data = []
def a_method(self):
# do something
Run Code Online (Sandbox Code Playgroud)
现在我用mixin创建一个A的子类:
class AWithMixin(A, Mixin):
pass
Run Code Online (Sandbox Code Playgroud)
我的问题是我想要调用A和Mixin的构造函数.我考虑给AWithMixin一个自己的构造函数,其中调用了super,但是超类的构造函数有不同的参数列表.什么是最佳分辨率?
python ×10
inheritance ×2
constructor ×1
ctypes ×1
default ×1
except ×1
hierarchy ×1
mixins ×1
oop ×1
parameters ×1
python-2.7 ×1
python-3.x ×1
quadratic ×1
recursion ×1
structure ×1
subclass ×1
super ×1
superclass ×1
tkinter ×1
try-catch ×1