'Python Decorator function to pass uncreated class variables

I need a decorator to help recreate the try... except blocks in the class below:

class Collect:
    def __init__(self, **kwargs):
        try:
            self.folder=kwargs['folder']
        except:
            pass
        try:
            self.service=kwargs['service']
        except:
            pass

I want to be able to instantiate Collect as either:

m = Collect(folder='Goodbye')

or

m = Collect(folder='Goodbye', service='Hello')

or

m = payload= m.Collect(service='Hello')

The class works as written above but I don't want to have lots of try:... except blocks. Without them I get a KeyError if I don't name all the class variables in the creation.

I have tried:

import json

def check(func):
    def inside(arg):
        try:
            arg
        except:
            arg=""
        val=func(arg)
        return val
    return inside

class Collect:
    with open('target.json', 'r') as data:
        target = json.loads(data.read())
    @check
    def __init__(self, **kwargs):
            #self.data=kwargs
            self.folder=kwargs['folder']
            self.service=kwargs['service']

But then none of the class variables will get created and I get an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: inside() got an unexpected keyword argument 'folder'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source