-1

Let's say I have a function with one / two option but what if I want to add more later on and more more? Is there a way to make it work without much hassle?

def json_response_message(status, message, option=(), option1=()):
    data = {
        'status': status,
        'message': message,
    }
    data.update(option)
    data.update(option1)
    return JsonResponse(data)

so I can use it like:

json_response_message(True, 'Hello', {'option': option})
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tsuna
  • 2,098
  • 6
  • 24
  • 46
  • You could make use of `*args` or `**kwargs`: https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/ – Scratch'N'Purr May 04 '17 at 20:35
  • @Scratch'N'Purr I tried it with `**kwargs` but when I input more options It would say something like `json_response_message() takes exactly 2 arguments (3 given)` – Tsuna May 04 '17 at 20:39
  • It's not clear what you want. If you want to pass an actual dictionary `json_response_message(True, 'Hello', {'option': option})`, just have a single `options=None` argument. If you want to pass arbitrary keyword arguments `json_response_message(True, 'Hello', option=option)`, then you need to use `**kwargs` *and unpack the dictionary when calling the function*: `json_response_message(True, 'Hello', **{'option': option})`. – jonrsharpe May 04 '17 at 20:45
  • @Tsuna Did you add `**kwargs` as a param in your function signature? e.g. `def json_response_message(status, message, **kwargs):` Also, if you do want to use keyword args, then you also have to pass the keyword identifier when you call your function: e.g. `json_response_message(True, 'Hello', option={'option': option})`... I'm assuming you want to pass dicts as your option arguments. – Scratch'N'Purr May 04 '17 at 20:45
  • You're looking at the wrong problem, rather than constantly extending a function, work out what you actually want and figure out how to address that, you shouldn't need to constantly extend a function – Sayse May 04 '17 at 21:05

2 Answers2

1

Try the following:

def json_response_message(status, message, **kwargs):
    data = {
        'status': status,
        'message': message,
    }

    data.update(kwargs)
    return JsonResponse(data)

And

json_response_message(True, 'Hello', option=option, option1=option1) # etc...

or, alternatively

json_response_message(True, 'Hello', **{"option": option, "option1": option1})

Hope this helps.

NS0
  • 6,016
  • 1
  • 15
  • 14
0

You can consider using variable length arguments *args. They are prefixed with * in function arguments. Try out this example.

def json_response_message(status, message, *options):
data = {
    'status': status,
    'message': message,
}
for option in options:
    data.update(option)
return JsonResponse(data)

You then would call your function with as many parameters as you wanted. json_response_message(True, 'Hello', {'option': option}, {'option1': option})

See http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/ for simplified further explanation.

yovsky
  • 111
  • 4