【问题标题】:Decorator to check input type in a class装饰器检查类中的输入类型
【发布时间】:2021-10-23 23:36:37
【问题描述】:

通过使用装饰器,我可以检查函数的变量类型。像这样的:

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'>

它适用于普通的简单。但我不知道如何在课堂上使用它:

class Test()
   @accepts(pd.DataFrame)
   def print_dataframe(df):
        print(df)

我无法初始化新课程并使用它。 请教我如何使用它

【问题讨论】:

    标签: python python-decorators


    【解决方案1】:

    正如所写,print_dataframe 是一个实例方法,因此您需要考虑到这一点:

    NO_CHECK = object()
    
    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):
                    if t is NO_CHECK:
                        continue
                    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
    
    # Using NO_CHECK because it's easier to skip checking the self argument
    # than it is to handle a check for that as-yet-undefined class Test
    class Test():
       @accepts(NO_CHECK, pd.DataFrame)
       def print_dataframe(self, df):
            print(df)
    

    或将print_dataframe 重新定义为静态方法:

       class Test():
           @staticmethod
           @accepts(pd.DataFrame)
           def print_dataframe(df):
                print(df)
    

    【讨论】:

    • 你不应该把object的括号去掉吗?
    • 不,这是故意的。我想要一个哨兵(与None 不同,因为mypy 已经建立了使用None 代替type(None) 进行类型检查的约定),并且object 该类是有效的(如果不太可能是used) 用于类型检查的类型。
    • 我的意思是,NO_CHECK = object。这将使 if 子句过时,因为任何对象都会通过类型检查测试。
    • 是的,但我想保持“根本不检查”与“接受任何类型的值”不同。我希望哨兵不是类型,而 object 的普通实例是最简单的方法。
    • 它不适用于@staticmethod,因为staticmethod 没有属性代码你有什么解决办法吗
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2015-06-25
    • 2020-07-13
    • 2019-11-03
    • 2014-04-08
    相关资源
    最近更新 更多