问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
# coding=utf-8
# 动态给类添加静态方法和类方法
class Person:
pass
@classmethod
def staticfunc():
print("staticfunc run!")
Person.staticfunc = staticfunc
Person.staticfunc()
@classmethod
def clsfunc(cls):
print("clsfunc run!")
Person.clsfunc = clsfunc
Person.clsfunc()
运行结果及报错内容
C:\Users\黄佳文\AppData\Local\Microsoft\WindowsApps\python3.10.exe D:/pythonProject/pythonProject/pythonProject/pythonProject/生成器和迭代器/06_动态给类添加静态方法和类方法.py
Traceback (most recent call last):
File "D:\pythonProject\pythonProject\pythonProject\pythonProject\生成器和迭代器\06_动态给类添加静态方法和类方法.py", line 13, in <module>
Person.staticfunc()
TypeError: staticfunc() takes 0 positional arguments but 1 was given
进程已结束,退出代码1
我的解答思路和尝试过的方法
我想要达到的结果