I went ahead and checked out some examples in other threads. I feel like I am still misunderstanding how exactly the interpreter handles **kwargs. I have the basics of *args understood and I feel pretty confident about how I think **kwargs is working. I just want to make sure its concrete.
So in my code example here, I need to understand why we use **kwargs and not just simply use kwargs? If I am understanding this correctly, the "*" or "wildcard" allows us to have a placeholder that can be replaced by any string or specified value (with some limitations I assume). This is confusing me because while learning the basics I have created my example and it doesn't exactly have the need for this type of **kwarg positional argument or it atleast as I understand. I don't exactly want to continue fiddling with it until I understand it more, it just seems to become more confusing when I do that, so I usually go back and read more. I understand that the wildcard is suppose to handle an unspecified number of arguments, that way you can have lists and dictionaries of an unknown size be sent through a function that can be called on with however many arguments you put.
So I guess, my question is this. Is this correct usage for **kwargs. Does anyone have a better way of explaining and understanding it's syntax and implementation. Thank you all for keeping this place going and providing an awesome community. I will continue reading while I wait for some responses.
Where the output spits out the last string of args and kwargs. Id like to separate them on two lines. I also want to make sure I am properly using **kwargs. If there is any advice on Do's and Don'ts I will definetely take it.
Trying to achieve this output:
So in my code example here, I need to understand why we use **kwargs and not just simply use kwargs? If I am understanding this correctly, the "*" or "wildcard" allows us to have a placeholder that can be replaced by any string or specified value (with some limitations I assume). This is confusing me because while learning the basics I have created my example and it doesn't exactly have the need for this type of **kwarg positional argument or it atleast as I understand. I don't exactly want to continue fiddling with it until I understand it more, it just seems to become more confusing when I do that, so I usually go back and read more. I understand that the wildcard is suppose to handle an unspecified number of arguments, that way you can have lists and dictionaries of an unknown size be sent through a function that can be called on with however many arguments you put.
So I guess, my question is this. Is this correct usage for **kwargs. Does anyone have a better way of explaining and understanding it's syntax and implementation. Thank you all for keeping this place going and providing an awesome community. I will continue reading while I wait for some responses.
kwargs = {"kwarg1": 1, "kwarg2": 2, "kwarg3": 3} def some_func(*args, **kwargs): print("*Args:", *args) some_func("AnArg1", "AnArg2", "AnArg3", kwargs)Quick Edit: I have been fiddling more and these are the examples I have come up with and read up on.
def greet_me(**kwargs): for key, value in kwargs.items(): print("{0} = {1}".format(key, value)) def test_args_kwargs(arg1, arg2, arg3): print("arg1:", arg1) print("arg2:", arg2) print("arg3:", arg3) args = ("two", 3, 5) test_args_kwargs(*args) kwargs = {"arg3": 3, "arg2": "two", "arg1": 5} test_args_kwargs(**kwargs) kwargs = {"kwarg1": 1, "kwarg2": 2, "kwarg3": 3} def some_func(*args, **kwargs): print("*Args:", *args) some_func("AnArg1", "AnArg2", "AnArg3", kwargs)The output is this:
Quote:arg1: two
arg2: 3
arg3: 5
arg1: 5
arg2: two
arg3: 3
*Args: AnArg1 AnArg2 AnArg3 {'kwarg1': 1, 'kwarg2': 2, 'kwarg3': 3}
Where the output spits out the last string of args and kwargs. Id like to separate them on two lines. I also want to make sure I am properly using **kwargs. If there is any advice on Do's and Don'ts I will definetely take it.
Trying to achieve this output:
Quote:arg1: two
arg2: 3
arg3: 5
arg1: 5
arg2: two
arg3: 3
*Args: AnArg1 AnArg2 AnArg3
**KWArgsg: {'kwarg1': 1, 'kwarg2': 2, 'kwarg3': 3}