在执行 raise 语句的同时创建异常类,无需事先定义异常类,方便快捷。
Project description
gqylpy-exception
在执行
raise
语句的同时创建异常类,无需事先定义异常类,方便快捷。例如,你想抛出一个名为NotUnderstandError
的异常, 导入import gqylpy_exception as ge
后直接执行raise ge.NotUnderstandError
即可。
pip3 install gqylpy_exception
使用 gqylpy_exception
创建异常类
import gqylpy_exception as ge
raise ge.AnError(...)
gqylpy_exception
可以创建任意名称的异常类。AnError
不是 gqylpy_exception
中内置的,它是在你的代码执行到 ge.
时创建的,魔化方法 __getattr__
的特性。你还可以通过魔法方法 __getitem__
获得它:
e: ge.GqylpyError = ge['AnError'](...)
是的,使用 gqylpy_exception
创建的异常类都继承 GqylpyError
,GqylpyError
继承内置的 Exception
。
还有一种用法,导入即创建:
from gqylpy_exception import AnError
raise AnError(...)
另外,gqylpy_exception
不会重复创建异常类,创建过的异常类将存入 ge.__history__
字典,当你再次创建时从这个字典中取值。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
gqylpy_exception-2.0.1.tar.gz
(10.4 kB
view hashes)
Built Distribution
Close
Hashes for gqylpy_exception-2.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d77d00b26e551dd4014c5178f225af7342a2d29ecc3d5f18305c1ea44a37ae4f |
|
MD5 | 97bcd7708d8786d5fc1c2a4b888c9b5e |
|
BLAKE2b-256 | e9121824438801d996fe7df7facda7c2c4c1fddd2a3e06a03a091170ff369eab |