Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- import math
- '''
- Создание пре-условия
- '''
- def pre(cond, mesg):
- def wrapper(func):
- @functools.wraps(func)
- def inner(*args, **kwargs):
- assert cond(*args, **kwargs), mesg # если cond==Faulse, то выведется сообщение msg
- return func(*args, **kwargs)
- return inner
- return wrapper
- @pre(lambda x: x>=0, "negative argument")
- def foo(x):
- return math.log(x)
- print(foo(4)) # 1.3862943611198906
- print(foo(-4)) # AssertionError: negative argument
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement