0

I have a class which contains configs method taking a lot of possible arguments which are setup by default to None/False. I want to create main function which would take name of argument and value and pass it to inner function call. How can I do it?

class Builder:
    def __init__(input_json):
        self.input_json = input_json

    def generate_main_field(self, field_value):
        self.input_json["main_field"] = field_value
        return self.input_json

    def configs(self, proxy=None, call=False, currency=None, phone=None, email=False, is_vi=False, provider=None, config_new_field=None)
        self.json_file[proxy] = "xxx"
        return self.json_file

def main_builder(json_file, *params)
    b = Builder(json_file)
    b.configs(*params)
    return json_file
PetrSevcik
  • 89
  • 1
  • 9
  • Can you provide clearer example of the behaviour you expect? – matszwecja Mar 16 '22 at 13:48
  • `def main_builder(.., **kwargs):` `b.configs(**kwargs)`…?! – deceze Mar 16 '22 at 13:49
  • you are so close! do `**params` instead of `*params` and make sure `params` is a dictionary – Nullman Mar 16 '22 at 13:49
  • To start, you need to at least save all those arguments as instance attributes; currently `Builder.configs` just ignores them. – chepner Mar 16 '22 at 13:49
  • Not working with double ** ``` json_dict = {"test":"test"} def main_builder(json_file, **params) b = Builder(json_file) b.configs(**params) return b.json_file print(main_builder(json_dict, {"proxy":"5.39.69.171:8888"})) TypeError: main_builder() takes 1 positional argument but 2 were given ``` – PetrSevcik Mar 16 '22 at 14:02
  • That's not how you *call* `**kwargs`. You want `main_builder(json_dict, proxy='5.39..')`. – deceze Mar 16 '22 at 14:06

0 Answers0