python : is decorators for method arguments possible?

标签 python methods arguments decorator

是否可以修饰方法参数?像这样的东西:

class SampleEntity (BaseEntity) :
    def someOperation (self, @Param(type="int", unit="MB")i, str) :
        pass

基本上,我希望开发人员能够指定有关类、属性、方法、参数等的元数据,我稍后可以处理这些元数据。对于类和方法,我可以使用装饰器。所以我想知道如何为方法参数执行此操作。

谢谢, 小

最佳答案

不,但是在 Python 3 中你可以使用 annotations .

def func(arg: Param(type = 'int', unit = 'MB')):
   pass

注释可以包含您想要的任何信息,语言并没有定义应该放在那里的内容。您可以稍后使用 func.__annotations__ dict 访问它们。

关于 python : is decorators for method arguments possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6122496/

相关文章:

python - xinetd 服务调用 python 脚本(无法正确执行)

android - 无法访问 fragment 方法中的 View 。它返回空对象

c++ - 在其实例类方法中调用 QMainWindow 方法

python - 从 cmd 行或 bash 脚本将参数传递给 python 脚本

python - Pandas 替换功能错误地更改了所有数据帧

python - 从树莓派上的 ADC 芯片读取原始音频值

javascript - 在 filter() 方法中返回值而不是对象

ruby-on-rails - Ruby 中参数前的 & 有什么作用?

function - 如何在 Perl 中检查函数参数是字符串还是数组

python - 如何修复尝试迁移manage.py时出现的错误