在执行 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.1.tar.gz
(12.5 kB
view hashes)
Built Distribution
Close
Hashes for gqylpy_exception-2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2166c9c13b4115da4003a36e458a4fe8391e3a92ca05f28a2cba635a4edf0b56 |
|
MD5 | 847b0c6b2ea01cc7c8a309ec202d83ca |
|
BLAKE2b-256 | 7d16fcb7add44a676d3797a936173713fc063827c310c28ea0d407aa85ad3e1b |