- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
谁能告诉我在这个 Django 中间件中收到警告背后的真正原因,我该如何解决这个问题?
我收到此消息“DeprecationWarning:BaseException.message 已从 Python 2.6 异常中弃用。class,exception.message,”
class GeneralMiddleware(object):
def process_exception(self, request, exception):
if exception.__class__ is SandboxError:
# someone is trying to access a sandbox that he has no
# permission to
return HttpResponseRedirect("/notpermitted/")
exc_type, value, tb = sys.exc_info()
data = traceback.format_tb(
tb, None) + traceback.format_exception_only(
exc_type, value)
msg = (
"Failure when calling method:\n"
u"URL:'%s'\nMethod:'%s'\nException Type:'%s'\n"
u"Error Message '%s'\nFull Message:\n%s"
% (request.get_full_path(), request.method,
exception.__class__, exception.message,
最佳答案
如果我没记错的话,当 Python 在 2.5(?) 中切换到新的 raise 语法时,他们摆脱了 message
成员,转而使用 args
元组。为了向后兼容,BaseException.message
实际上与 BaseException.args[0] if BaseException.args else None
相同,但您不应在新代码中使用它.
因此,将 message
更改为 args
(如果您需要所有参数)或 args[0]
(或者,如果您担心可能没有 args,防止 ()
的更高级版本),具体取决于您想要的。
此更改的原因是,有了新式异常,raise
或 except
不再有魔法;您只是在 raise
语句中调用异常类的构造函数,并在 except
语句中的变量中捕获异常。所以:
try:
raise MyException('Out of cheese error', 42)
except Exception as x:
print x.args
这将打印 ('Out of cheese error', 42)
。如果您只有 print x.message
,您只会得到 'Out of cheese error'
。因此,过去必须做一些花哨的事情来将错误代码作为一个单独的成员携带等等的 Exception 子类可以得到简化;事实上,整件事归结为:
class BaseException(object):
def __init__(self, *args):
self.args = args
关于python - DeprecationWarning : BaseException. 消息已从 Python 2.6 exception.__class__, exception.message,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063212/
在 Python 的文档中 __class__被描述为一个属性。在对象中 type (元类),__class__似乎是一种方法。 如果我们这样做: >>> class Foo: pas
我有一系列的类,它们都继承自共享一个基类的几个可能的父类中的一个。每个类都有一个基于基类 parameters 的类作用域 Dict[str, object] parameters from copy
这个问题在这里已经有了答案: Why doesn't 2.__add__(3) work in Python? (2 个答案) 关闭 3 年前。 整数是唯一不支持.__class__操作的类型吗?例
我最近正在单步调试 CPython 源代码,特别是查看 symbol table entry编译期间的一个类。 我遇到了 typedef struct _symtable_entry 的以下条目结构:
我想测试一个对象是否是一个类的实例,并且只有这个类(没有子类)。我可以这样做: obj.__class__ == Foo obj.__class__ is Foo type(obj) == Foo t
有以下服务器代码(我正在定义一个继承自套接字的类,并尝试使用该类调用客户端套接字上的发送方法): import socket class LogSocket(socket.socket): d
python 依赖于 cell 中的 __class__ 变量来进行 super() 调用。它从第一个堆栈帧中的free 变量获取此单元格。 奇怪的是,这个变量不在 locals() 中,而是在您从
我对设置对象的属性和在对象的 __class__ 属性上设置属性之间的区别感到有点困惑。粗略地说,obj.attr 与 obj.__class__.attr。它们有什么区别? 前者是在一个实例上设置一
在 pinax Userdict.py 中: def __getitem__(self, key): if key in self.data: return s
这个问题在这里已经有了答案: How to avoid infinite recursion with super()? (1 个回答) 关闭 5 年前。 我在 WTForms 中处理继承。我有下一
Django 源代码: def new_method_proxy(func): def inner(self, *args): if self._wrapped is empt
使用 __class__ 在类中创建新实例是个好主意吗? 以下代码是执行此操作的示例: from collections import namedtuple _Position = namedtupl
我希望能够做到: >>> class a(str): ... pass ... >>> b = a() >>> b.__class__ = str Traceback (most recent
我有一个关于 python 中的 __class__ 的问题。 文档说 __class__ 是类实例所属的类。于是我进行了一系列的实验: class counter: count = 0
也许这是一个微不足道的问题。在 python 下,为什么 self.__class__.__bases__ 没有回溯到 object?我的只显示了类(class)之上的单亲 parent 的元组。 最
我创建了这样的类来解决签名和 feature_names 的问题: import copy from sklearn.feature_selection import VarianceTreshold
我已阅读 Why is Python 3.x's super() magic?并了解在方法中使用 super 或 __class__ 会自动为该方法创建一个 __class__ 单元格变量: clas
我需要从类中获取类名: class Cls: notation = None def __init__(self): notation = self.__class__
我试图通过从该类派生并覆盖/添加某些方法来扩展用 C 编写的模块类的功能*。 问题是他的模块创建了我试图在不同地方扩展的类的实例。因此,我需要创建一个转换方法,将模块提供的基类转换为我自己的具有附加功
class Foo: pass >>> f = test.Foo() 让我们看看类实例...... >>> dir(f) ['__add__', [__class__] ...] 哦哦!让我们看看
我是一名优秀的程序员,十分优秀!