python - 异常子类必须在类之外吗?

标签 python python-2.7

我必须对我的类(class)中的无效参数提出异常。

class MyFunc(object):
    class InvalidArgument(ValueError):
         pass

所以我从 ValueError 中继承了子类。将其放入类中更有意义,因为我不希望我的异常类与库或其他模块中的其他异常发生冲突。然而,Python 中有时不鼓励使用内部类。我应该将 InvalidArgument 放在类内或类外的什么位置。

最佳答案

Does exception sub class has to be outside class?

没有。但是您应该将其放在类之外的模块级别范围内。

I don`t want my exception class to collide with other exceptions from library or other module.

不会的。它将由模​​ block 名称命名。

关于python - 异常子类必须在类之外吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48694145/

相关文章:

javascript - 无法访问从 html javascript 上的 python render_template (jinja2/webapp2) 传递的参数

python - 在 Pillow 中确定带有换行符的字符串的像素高度

python - 类型错误 : force_login() missing 1 required positional argument: 'user'

python - Pythonista:如何显示UIAlertController?

python - 轻量级python库通过邮政编码查询城市/州名?

python-2.7 - os.rename 模块似乎删除了输出目录中的文件

python - CSV、Python 中的字符串大小写运算

python - 使用用户输入打印数学: Can't convert float to str implicitly

Python:为什么不能使用 self.variable 作为方法中的可选参数?

python - 如何在 python 中对字符串使用格式说明符