我想知道在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中创建自己的BadValueErrors,这是很好理解的,还是有其他的首选方法?
You would most likely use ValueError (raise ValueError() in full) in this case, but it depends on the type of bad value. For example, if you made a function that only allows strings, and the user put in an integer instead, you would you TypeError instead. If a user inputted a wrong input (meaning it has the right type but it does not qualify certain conditions) a Value Error would be your best choice. Value Error can also be used to block the program from other exceptions, for example, you could use a ValueError to stop the shell form raising a ZeroDivisionError, for example, in this function:
def function(number):
if not type(number) == int and not type(number) == float:
raise TypeError("number must be an integer or float")
if number == 5:
raise ValueError("number must not be 5")
else:
return 10/(5-number)
附注:python内置异常列表,请点击此处:
https://docs.python.org/3/library/exceptions.html(这是官方的python数据库)