简体   繁体   English

错误异常必须从 BaseException 派生,即使它确实存在(Python 2.7)

[英]Error exception must derive from BaseException even when it does (Python 2.7)

What's wrong with the following code (under Python 2.7.1):以下代码有什么问题(在 Python 2.7.1 下):

class TestFailed(BaseException):
    def __new__(self, m):
        self.message = m
    def __str__(self):
        return self.message

try:
    raise TestFailed('Oops')
except TestFailed as x:
    print x

When I run it, I get:当我运行它时,我得到:

Traceback (most recent call last):
  File "x.py", line 9, in <module>
    raise TestFailed('Oops')
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType

But it looks to me that TestFailed does derive from BaseException .但在我看来那TestFailed确实派生自BaseException

__new__ is a staticmethod that needs to return an instance. __new__是一个需要返回实例的staticmethod

Instead, use the __init__ method:相反,使用__init__方法:

class TestFailed(Exception):
    def __init__(self, m):
        self.message = m
    def __str__(self):
        return self.message

try:
    raise TestFailed('Oops')
except TestFailed as x:
    print x

Others have shown you how to fix your implementation, but I feel it important to point out that the behavior you are implementing is already the standard behavior of exceptions in Python so most of your code is completely unnecessary.其他人已经向您展示了如何修复您的实现,但我认为重要的是要指出您正在实现的行为已经是Python 中异常标准行为,因此您的大部分代码完全没有必要。 Just derive from Exception (the appropriate base class for runtime exceptions) and put pass as the body.只需从Exception (运行时异常的适当基类)派生,并将pass作为主体。

class TestFailed(Exception):
    pass

Use __init__() instead of __new__() to "initialize" classes.使用__init__()而不是__new__()来“初始化”类。 In most cases overriding __new__ is not necessary.在大多数情况下,不需要覆盖__new__ It is called before __init__ during object creation.它在对象创建期间在__init__之前调用。

See also Python's use of __new__ and __init__?另请参阅Python 对 __new__ 和 __init__ 的使用?

The __new__ implementation should return an instance of the class, but it's currently returning None (by default). __new__实现应该返回类的一个实例,但它当前返回None (默认情况下)。

However, it looks like you should be using __init__ here, rather than __new__ .但是,看起来您应该在这里使用__init__而不是__new__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常必须从 BaseException 派生 - Exceptions must derive from BaseException Flask / Python API:TypeError:异常必须从 BaseException 派生 - Flask / Python API : TypeError: exceptions must derive from BaseException 为什么建议在Python中派生Exception而不是BaseException类? - Why is it recommended to derive from Exception instead of BaseException class in Python? 失败:异常必须从 BaseException 派生 - failed: exceptions must derive from BaseException TypeError:异常必须从 BaseException 派生 - TypeError: exceptions must derive from BaseException 我得到“TypeError:异常必须从BaseException派生”,即使我确实定义了它 - I get “TypeError: exceptions must derive from BaseException” even though I did define it Python,MetaClasses,HTTPException,__init_subclass__ - TypeError:异常必须从 BaseException 派生 - Python, MetaClasses, HTTPException, __init_subclass__ - TypeError: exceptions must derive from BaseException 为什么需要从 BaseException 派生自定义异常? - Why do you need to derive your custom exception from BaseException? redis Python 捕获不继承自 BaseException 的异常 - redis Python catching exception that doesn't inherit from BaseException TypeError:异常必须从 BaseException 派生。 “我正试图在比赛结束后暂停我的比赛。” - TypeError: exceptions must derive from BaseException. "I'm trying to pause my game after the game over."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM