|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def type_check(correct_type):
- def outer(func):
- def inner(arg):
- if(type(arg) == correct_type):
- return func(arg)
- else:
- return "参数类型错误!"
- return inner
- return outer
-
-
- print("<<<--- 测试整数 --->>>")
-
- @type_check(int)
- def double(x):
- return x * 2
- print(double(2))
复制代码
@type_check(int)这个等于 double = type_check(int) 等于 double = outer, double(2) 等于outer(2) 那不对吧 没想明白这里
本帖最后由 lxping 于 2022-12-1 22:46 编辑
double函数是传给outer(func)中的形参func的
- @type_check(int)
- def double(x):
- return x * 2
- #相当于
- def double(x):
- return x * 2
- double = type_check(correct_type=int)(func=double)
- #实际上就是最后调用的就是double
- double(2) #再将参数2传给形参x
复制代码
|
|