如何使用Python装饰器检查函数参数?

49 投票
10 回答
53775 浏览
提问于 2025-04-17 18:26

我想定义一些通用的装饰器,用来在调用某些函数之前检查参数。

大概是这样的:

@checkArguments(types = ['int', 'float'])
def myFunction(thisVarIsAnInt, thisVarIsAFloat)
    ''' Here my code '''
    pass

附带说明:

  1. 类型检查只是用来举例说明
  2. 我在使用Python 2.7,不过Python 3.0也很有意思

2021年补充:有趣的是,类型检查在长远来看并没有违背Python的设计理念,因为出现了类型提示mypy

10 个回答

12

正如你所知道的,光凭参数的类型来拒绝一个参数并不是很符合Python的风格。
Python的风格更倾向于“先试着处理一下”。
所以我更喜欢用一个装饰器来转换参数。

def enforce(*types):
    def decorator(f):
        def new_f(*args, **kwds):
            #we need to convert args into something mutable   
            newargs = []        
            for (a, t) in zip(args, types):
               newargs.append( t(a)) #feel free to have more elaborated convertion
            return f(*newargs, **kwds)
        return new_f
    return decorator

这样,你的函数就能接收到你期望的类型。
但是如果这个参数的表现像浮点数一样,它也是可以被接受的。

@enforce(int, float)
def func(arg1, arg2):
    return arg1 * arg2

print (func(3, 2)) # -> 6.0
print (func('3', 2)) # -> 6.0
print (func('three', 2)) # -> ValueError: invalid literal for int() with base 10: 'three'

我用这个技巧(配合合适的转换方法)来处理向量
我写的很多方法都期望接收MyVector类,因为它有很多功能;但有时候你只是想写

transpose ((2,4))
19

在Python 3.3中,你可以使用函数注解和检查工具:

import inspect

def validate(f):
    def wrapper(*args):
        fname = f.__name__
        fsig = inspect.signature(f)
        vars = ', '.join('{}={}'.format(*pair) for pair in zip(fsig.parameters, args))
        params={k:v for k,v in zip(fsig.parameters, args)}
        print('wrapped call to {}({})'.format(fname, params))
        for k, v in fsig.parameters.items():
            p=params[k]
            msg='call to {}({}): {} failed {})'.format(fname, vars, k, v.annotation.__name__)
            assert v.annotation(params[k]), msg
        ret = f(*args)
        print('  returning {} with annotation: "{}"'.format(ret, fsig.return_annotation))
        return ret
    return wrapper

@validate
def xXy(x: lambda _x: 10<_x<100, y: lambda _y: isinstance(_y,float)) -> ('x times y','in X and Y units'):
    return x*y

xy = xXy(10,3)
print(xy)

如果出现验证错误,会打印:

AssertionError: call to xXy(x=12, y=3): y failed <lambda>)

如果没有验证错误,会打印:

wrapped call to xXy({'y': 3.0, 'x': 12})
  returning 36.0 with annotation: "('x times y', 'in X and Y units')"

你可以用一个普通的函数来代替lambda表达式,这样在断言失败时可以显示出函数的名字。

54

来自于函数和方法的装饰器

Python 2

def accepts(*types):
    def check_accepts(f):
        assert len(types) == f.func_code.co_argcount
        def new_f(*args, **kwds):
            for (a, t) in zip(args, types):
                assert isinstance(a, t), \
                       "arg %r does not match %s" % (a,t)
            return f(*args, **kwds)
        new_f.func_name = f.func_name
        return new_f
    return check_accepts

Python 3

在Python 3中,func_code变成了__code__,而func_name变成了__name__

def accepts(*types):
    def check_accepts(f):
        assert len(types) == f.__code__.co_argcount
        def new_f(*args, **kwds):
            for (a, t) in zip(args, types):
                assert isinstance(a, t), \
                       "arg %r does not match %s" % (a,t)
            return f(*args, **kwds)
        new_f.__name__ = f.__name__
        return new_f
    return check_accepts

用法:

@accepts(int, (int,float))
def func(arg1, arg2):
    return arg1 * arg2

func(3, 2) # -> 6
func('3', 2) # -> AssertionError: arg '3' does not match <type 'int'>

arg2可以是int(整数)或者float(浮点数)

撰写回答