Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python double asterisk that is not **kwargs but rather gets used on the function call itself

was watching here:

https://youtu.be/F5mRW0jo-U4?t=9267

he proceeds to write the call:

Product.objects.create(**my_form.cleaned_data)

Can someone explain what is the difference between

Product.objects.create(my_form.cleaned_data)

and

Product.objects.create(**my_form.cleaned_data)

like image 540
Toskan Avatar asked Oct 25 '25 02:10

Toskan


1 Answers

Assuming that my_form.cleaned_data is a dict, or some other kind of mapping, then create(**my_form.cleaned_data) will pass all the dict's entries as keyword arguments. So:

def my_fun(a=None, b=None):
    print(a)
    print(b)

my_dict = {'a':1, 'b':2}

my_fun(**my_dict)

would print:

1
2
like image 200
brunns Avatar answered Oct 26 '25 17:10

brunns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!