python:在类中定义装饰器函数时出错,该如何解决?
伊谢尔伦
伊谢尔伦 2017-04-18 10:10:45
[Python讨论组]
#学生类
class Stu:
    def __init__(self,name,grade):
        self.name=name    #名字
        self.grade=grade    #分数
    
    def printout(self):
        print("name:{0} grade:{1}".format(str(self.name),str(self.grade)))    #格式化输出

    def plusDouble(self,plus_fun):    #装饰器函数
        def inner():
            return str(plus_fun())+"分"
        return inner

    @plusDouble 
    def plus(self,num):
        self.grade=self.grade+num
        return self.grade    #返回加分后的结果

#测试
student=Stu("zhangsan",20)
student.printout()
student.plus(20)
student.printout()

报错的结果:

Traceback (most recent call last):
  File "E:\eclipse\eclipse\plugins\org.python.pydev_4.5.5.201603221110\pysrc\pydevd.py", line 1529, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "E:\eclipse\eclipse\plugins\org.python.pydev_4.5.5.201603221110\pysrc\pydevd.py", line 936, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "E:\eclipse\eclipse\plugins\org.python.pydev_4.5.5.201603221110\pysrc\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:\workplace\PlayPy\Play.py", line 14, in <module>
    class Stu:
  File "E:\workplace\PlayPy\Play.py", line 27, in Stu
    @plusDouble 
TypeError: plusDouble() missing 1 required positional argument: 'plus_fun'

TypeError: plusDouble() missing 1 required positional argument: 'plus_fun'
这该怎么办?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(3)
PHP中文网
# coding: utf-8

def plusDouble(plus_fun):    #装饰器函数
    def inner(self, num):
        return str(plus_fun(self, num))+"分"
    return inner

class Stu:
    def __init__(self,name,grade):
        self.name=name    #名字
        self.grade=grade    #分数

    def printout(self):
        print("name:{0} grade:{1}".format(str(self.name),str(self.grade)))    #格式化输出

    @plusDouble
    def plus(self,num):
        self.grade=self.grade+num
        return self.grade    #返回加分后的结果

#测试
student=Stu("zhangsan",20)
student.printout()
student.plus(20)
student.printout()
伊谢尔伦

报错很清楚啊… plusDouble少一个参数。作为一个类方法的plusDouble是有两个参数的… 它作为装饰器的时候你只传了一个参数进去,plus = plusDouble(plus) 当然是少一个参数了。

巴扎黑
#学生类
class Stu:
    def __init__(self,name,grade):
        self.name=name    #名字
        self.grade=grade    #分数
    
    def printout(self):
        print("name:{0} grade:{1}".format(str(self.name),str(self.grade)))    #格式化输出

    def _plusDouble(plus_fun):    #装饰器函数
        def inner(self,num):
            return str(plus_fun(self, num))+"分"
        return inner

    @_plusDouble 
    def plus(self,num):
        self.grade=self.grade+num
        return self.grade    #返回加分后的结果

#测试
student=Stu("zhangsan",20)
student.printout()
student.plus(20)
student.printout()
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号