annotation: a note or notes added to a book or text giving explanations or comments; the act of adding these notes
class NonIntArgumentException(Exception):
pass
def handleNonIntArguments(func):
def wrapper(*args):
for item in args:
if type(item) is not int:
raise NonIntArgumentException()
return func(*args)
return wrapper
NonIntArgumentException is a custom exception class.
handleNonIntArguments(func) returns wrapper, which replaces the original function.
wrapper(*args) accepts any number of positional arguments.
It checks each argument and raises the custom exception if any is not exactly an int.
If all arguments are integers, it calls the original func(*args).
Example:
@handleNonIntArguments
def add(a, b):
return a + b
add(2, 3) # returns 5
add(2, "3") # raises NonIntArgumentException
add(2, 3.0) # raises NonIntArgumentException
Perplexity.ai
沒有留言:
發佈留言