处理方法d中的cls和self

2024-04-24 21:09:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下问题,我试图找到最适合Python的方法。我有一个解决办法,但它根本感觉不到Python的力量。也许你知道一个更优雅的解决方案?在

问题是

作为一个装修师,我想

  1. 查找函数参数的名称
  2. 从JSON字符串中提取参数
  3. 按正确的顺序将它们传递给函数

decorator应该是所有函数类型的泛型,它们是staticmethodclassmethodinstance method(带self)或{}(类外)。我编了最后两个名字。请评论,如果你知道你如何实际调用这些函数。谢谢您。这带来了一个问题:传递给decorator的参数有时包含一个类对象或一个类实例,有时不包含。在

假设我的项目如下:

import inspect

@the_decorator
def fun_method(name, password):
    pass

class ExampleClass:
    @staticmethod
    @the_decorator
    def static_method(name, password):
        pass

    @classmethod
    @the_decorator
    def class_method(cls, name, password):
        pass

    @the_decorator
    def instance_method(self, name, password):
        pass

我目前的解决方案

^{pr2}$

Tags: theinstance函数nameself参数defdecorator