0

I made my own class for some data processing.

Here is my concept of class module condition

  1. I want to take all user input variables including **kwargs from __init__ only

  2. __init__ doing pre processing which need **kwargs for one of the input variables

  3. also need additional data processing function inside of class which need **kwargs

  4. also need __call__ method which need **kwargs

to satisfy all my conditions, in my view I need to make **kwargs as something like self.**kwargs. So, how can I make keyword arguments to self variables?

M.K Kim
  • 1
  • 1
  • Keyword arguments are a syntax feature mostly, while you are talking about actual data handling. This probably doesn't make sense. Anyhow, your question is unclear to me. In order to improve it, I suggest you provide example code how you would do it without making `kwargs` a member, just for comparison. – Ulrich Eckhardt Mar 06 '18 at 12:35
  • 1) you can just pass `**kwargs` to the according function call 2) if required you may create a local `dict( **kwargs)` 3) don't use `kwargs` if not really necessary. An explicit list of arguments is by far more readable and more easy considering code documentation. – mikuszefski Mar 06 '18 at 12:53

2 Answers2

2

When you write **foo in the parameters of a function, foo is passed to the function as a perfectly ordinary dict. If you want to save that dict, just copy it to a member variable:

class Foo:
    def __init__(self,**kwargs) :
        self.kwargs = kwargs

    def function():
        if 'Arg1' in self.kwargs:
            return self.kwargs['Arg1']
        else
            return 0
0

Do you mean something like this?

self.__dict__.update(kwargs)

source: https://stackoverflow.com/a/8685206/7762936