简体   繁体   中英

How to check for class attribute initialization with a decorator?

Consider the following code.

def check(data):
    def decorator(function):
        def wrapper(*args, **kwargs):
            if data not in ["toto", "tata"]:
                raise ValueError("Wrong argument")
            result = function(*args, **kwargs)
            return result
        return wrapper
    return decorator


class Foo:
    
    @check
    def __init__(data):
        self.data = data
         
         
Foo(data="t")

I would like to decorate the __init__ function of a class, but I get an exception:

Traceback (most recent call last):
  File "<string>", line 19, in <module>
TypeError: decorator() got an unexpected keyword argument 'data'

You're mixing up which function receives what. check is the decorator which receives the function you're decorating, not data . wrapper is the function which will receive data . You don't need the third function in there. So:

def check(function):
    def wrapper(self, data):
        if data not in ["toto", "tata"]:
            raise ValueError("Wrong argument")

        return function(self, data)

    return wrapper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM