class Exception
python 2.x 中的签名是什么?
我希望对它进行子类化并添加我自己的参数,同时也正确调用super
.
以下代码有效:
class FooError(Exception):
def __init__(self, msg, x):
super(FooError, self).__init__(msg)
self.x = x
Run Code Online (Sandbox Code Playgroud)
你那里有的都很好。交替,
class FooError(Exception):
def __init__(self, msg, x):
Exception.__init__(self, msg)
self.x = x
Run Code Online (Sandbox Code Playgroud)
从文档来看:
派生类中的重写方法实际上可能想要扩展而不是简单地替换同名的基类方法。有一个简单的方法可以直接调用基类方法:只需调用 BaseClassName.methodname(self,arguments) 即可。这有时对客户也很有用。(请注意,仅当基类可在全局范围内作为 BaseClassName 访问时,这才有效。)
归档时间: |
|
查看次数: |
1690 次 |
最近记录: |