0

I have a function which I want to pass two optional parameters. I have read through but somehow I am not able to make it work though.

What am I missing here?

I have something like this

def json_response_message(status, message, option_key, option_value):
    data = {
        'status': status,
        'message': message,
    }
    if option_key:
        data[option_key] = option_value
    return JsonResponse(data)

the last two parameters I want it to be optional.

I have seen there it can be done by doing

def json_response_message(status, message, option_key='', option_value=''):

but I don't really want to do it this way and saw that there is a way to pass *args and **kwargs but it couldn't get it to work though.

I am just stuck at putting the optional parameters but not sure how to call and use them. I read through some posts and its' easily be done and call by using a for loop but somehow it just didn't work for me

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

    return JsonResponse(data)

I want add extra parameters into my data return such as...

    user = {
        'facebook': 'fb',
        'instagram': 'ig'
    }

    return json_response_message(True, 'found', 'user', user)
2
  • 2
    "but I don't really want to do it this way" - why not? Commented Apr 14, 2017 at 23:37
  • Which of these two is your expected output? {'status': status, 'message': message, 'user': { 'facebook': 'fb', 'instagram': 'ig'} } or {'status': status, 'message': message, 'facebook': 'fb', 'instagram': 'ig'} (i.e. you want to append optional parameter to response as dictionary or do you want to return flat structure, but add multiple key-value pairs through optional function args)? Commented Apr 14, 2017 at 23:53

2 Answers 2

2

You want something like this, I suppose:

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

    # assuming options is now just a dictionary or a sequence of key-value pairs
    data.update(options)

    return data

And you can use it like this:

user = {
    'facebook': 'fb',
    'instagram': 'ig'
}
print(json_response_message(True, 'found', user))
Sign up to request clarification or add additional context in comments.

5 Comments

this is a better answer than mine, I suggest going with this.
this is superb, just something came into my mind. is there possible way to put more than one dictionary in? like having two / three options or more?
@Dora Yeah, you can have as many optional parameters as you want. option1=(), option2=(),...
guess it was my system messed up, it was giving me errors doing it but after restarting it worked well. But if I have like more and more and more, is there a better way. I have been reading about **kwargs which seems helpful but somehow I couldn't get around to use it in this situation though
@Dora you could use **kwargs, but I would just stick with maybe *args. The only difference is the container for the variable number of arguments, the former gives you a dictionary of arguments, the latter a tuple of arguments. If you are just going to iterate over them in sequence and update some data structure, a tuple might be exactly what you need.
0
def json_response_message(status, message, *args):

    #input validation
    assert len(args) == 0 or len(args) == 2

    # add required params to data
    data = {
    'status': status,
    'message': message,
    }

    # add optional params to data if provided
    if args:
      option_key = args[0]
      option_value = args[1]
      data[option_key] = option_value      

    return data

print(json_response_message(True, 'found', 'user', user))

{'user': 'jim', 'status': True, 'message': 'found'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.