TypeCast/aartur | TypeCast/dakyun | ||||
---|---|---|---|---|---|
f | 1 | from functools import wraps | f | 1 | from functools import wraps |
2 | 2 | ||||
3 | 3 | ||||
n | 4 | def cast(t): | n | 4 | def cast(type): |
5 | def decorator(f): | 5 | def decor(f): | ||
6 | @wraps(f) | 6 | @wraps(f) | ||
n | 7 | def new_f(*args): | n | 7 | def wrapper(*args): |
8 | res = f(*args) | 8 | res = f(*args) | ||
n | 9 | return t(res) | n | 9 | return type(res) |
10 | return new_f | ||||
11 | return decorator | ||||
12 | 10 | ||||
t | t | 11 | return wrapper | ||
12 | |||||
13 | return decor | ||||
14 | |||||
15 | |||||
16 | # @cast(int) | ||||
17 | # def fun(a, b): | ||||
18 | # return a * 2 + b | ||||
19 | # print(fun(12, 34) * 2) | ||||
20 | # print(fun("12", "34") * 2) | ||||
21 | # print(fun(12.765, 34.654) * 2) | ||||
22 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|