鱼C论坛

 找回密码
 立即注册
查看: 32|回复: 7

[技术交流] 看看我的PYTHON

[复制链接]
发表于 2026-9-27 12:54:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
def add(x, y):
        return x + y
import functools
print(functools.reduce(add, [1, 2, 3, 4, 5]))
print(functools.reduce(lambda x,y:x*y, range(1, 11)))



square = functools.partial(pow, exp=2)
print(square(2))
cube = functools.partial(pow, exp=3)
print(cube(3))



import time
import functools
def time_master(func):
        @functools.wraps(func)
        def call_func():
                print("开始运行程序...")
                start = time.time()
                func()
                stop = time.time()
                print("结束程序运行...")
                print(f"一共耗费了 {(stop-start):.2f} 秒")
        return call_func
def myfunc():
        time.sleep(2)
        print("I love FishC.")
myfunc = time_master(myfunc)
myfunc()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2026-9-27 12:54:36 | 显示全部楼层
鱼油,你的代码整体思路不错,但有一个地方会直接报错:

print(functools.reduce(add, )) 这里 reduce 只传了函数,没有传可迭代对象,会抛出 TypeError。  
应该写成类似这样:


  1. print(functools.reduce(add, [1, 2, 3, 4]))
复制代码


其他部分基本可以运行,但有两个小建议:

1. functools.partial(pow, exp=2) 依赖 Python 版本支持 pow 的关键字参数 exp。更稳妥的写法是固定位置参数,例如:

  1. square = functools.partial(pow, 2)
  2. print(square(3))  # 2 的 3 次方
复制代码

不过你原本的写法在 Python 3.8+ 通常也能跑。

2. 装饰器 time_master 里的 call_func 没有接收参数,如果以后装饰带参数的函数会出错。建议改成:

  1. def call_func(*args, **kwargs):
  2.     ...
  3.     func(*args, **kwargs)
  4.     ...
复制代码


修正后的完整代码可以参考:


  1. import functools
  2. import time

  3. def add(x, y):
  4.     return x + y

  5. print(functools.reduce(add, [1, 2, 3, 4]))
  6. print(functools.reduce(lambda x, y: x * y, range(1, 11)))

  7. square = functools.partial(pow, exp=2)
  8. print(square(2))
  9. cube = functools.partial(pow, exp=3)
  10. print(cube(3))

  11. def time_master(func):
  12.     @functools.wraps(func)
  13.     def call_func(*args, **kwargs):
  14.         print("开始运行程序...")
  15.         start = time.time()
  16.         func(*args, **kwargs)
  17.         stop = time.time()
  18.         print("结束程序运行...")
  19.         print(f"一共耗费了 {(stop - start):.2f} 秒")
  20.     return call_func

  21. def myfunc():
  22.     time.sleep(2)
  23.     print("I love FishC.")

  24. myfunc = time_master(myfunc)
  25. myfunc()
复制代码


这样就能正常运行啦,继续加油!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-9-27 12:56:00 | 显示全部楼层
我感觉我一直在发表
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2026-9-27 12:56:07 | 显示全部楼层
贝尔0703 发表于 2026-09-27 12:56
我感觉我一直在发表


哈哈,你说的“发表”是不是指 print 呀?在 Python 里 print 就是把内容输出到屏幕,确实有点像在“发表”。

你代码里 print 用了不少次,所以会有一种“一直在发表”的感觉。如果想让输出清爽一点,可以先把结果存起来,最后统一 print:


  1. import functools

  2. def add(x, y):
  3.     return x + y

  4. result1 = functools.reduce(add, )
  5. result2 = functools.reduce(lambda x, y: x * y, range(1, 11))
  6. print(result1, result2)
复制代码


不过注意,functools.reduce(add, ) 这里还是缺了可迭代对象,会报错哦。记得补上类似 [1, 2, 3] 这样的参数。

继续加油,多发“有用的表”!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-9-27 12:56:35 | 显示全部楼层
谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2026-9-27 12:56:39 | 显示全部楼层


不客气,鱼油!  
继续加油,有问题随时来问~

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2026-9-27 19:03:14 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2026-10-1 16:07:35 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-10-10 23:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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