我想知道在 Python 中指示无效参数组合的最佳实践。我遇到过一些你有这样的功能的情况:
def import_to_orm(name, save=False, recurse=False):
"""
:param name: Name of some external entity to import.
:param save: Save the ORM object before returning.
:param recurse: Attempt to import associated objects as well. Because you
need the original object to have a key to relate to, save must be
`True` for recurse to be `True`.
:raise BadValueError: If `recurse and not save`.
:return: The ORM object.
"""
pass
唯一的烦恼是每个包都有自己的,通常略有不同BadValueError
。我知道在 Java 中存在java.lang.IllegalArgumentException
- 是否每个人都将在 Python 中创建自己的BadValueError
s 或者是否有另一种首选方法?