鱼C论坛

 找回密码
 立即注册
查看: 5105|回复: 3

[已解决]作业 046课 动动手 第一题的问题

[复制链接]
发表于 2023-1-12 14:17:09 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-1-12 14:18 编辑

@type_check(int)
def double(x):

相当于 double = type_check(int)(double)


  1. def type_check(correct_type):
  2.     def outer(func):
  3.         def inner(arg):
  4.             if(type(arg) == correct_type):
  5.                 return func(arg)
  6.             else:
  7.                 return "参数类型错误!"
  8.         return inner
  9.     return outer
复制代码

中不难看出:
type_check(int) 返回的是 outer,而且 correct_type 为 int,即

type_check(int) 相当于
  1. def outer(func):
  2.     def inner(arg):
  3.         if(type(arg) == int):
  4.             return func(arg)
  5.         else:
  6.             return "参数类型错误!"
  7.     return inner
复制代码


type_check(int)(double) 就相当于 outer(double),即
  1. def inner(arg):
  2.     if(type(arg) == int):
  3.         return double(arg)
  4.     else:
  5.         return "参数类型错误!"
复制代码


通过 @type_check(int) 的修饰,其实 double 已经被替换成了上面的这个 inner 函数了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-12 16:37:10 | 显示全部楼层
caeser 发表于 2023-1-12 15:55
那么这个例子里面的 exp 和 base 返回的不是数据类型呢?

上面那个 correct_type 传进去的是 int 呀,这里的 exp 和 base 传进去的 3,

这是看你传进去的参数的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-2 05:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表