Can anyone explain the difference when unpacking the dictionary using single or double asterisk? You can mention their difference when used in function parameters, only if it is relevant here, which I don't think so.
However, there may be some relevance, because they share the same asterisk syntax.
def foo(a,b)
return a+b
tmp = {1:2,3:4}
foo(*tmp) #you get 4
foo(**tmp) #typeError: keyword should be string. Why it bothers to check the type of keyword?
Besides, why the key of dictionary is not allowed to be non-string when passed as function arguments in THIS situation? Are there any exceptions? Why they design Python in this way, is it because the compiler can't deduce the types in here or something?
Thanks!
For numeric data types double asterisk (**) is defined as exponentiation operator >>> a=10; b=2 >>> a**b 100 >>> a=1.5; b=2.5 >>> a**b 2.7556759606310752 >>> a=3+2j >>> b=3+5j >>> a**b (-0.7851059645317211+2.350232331971346j)
The * operator is an unpacking operator that will unpack the values from any iterable object, such as lists, tuples, strings, etc… And that's it! The asterisk, *, or unpacking operator, unpacks num_list, and passes the values, or elements, of num_list as separate arguments to the num_sum function.
In a function call the '**' unpacks data structure of dictionary into positional or keyword arguments to be received by function definition. In a function definition the '*' packs positional arguments into a tuple. In a function definition the '**' packs keyword arguments into a dictionary.
It is used to pass a variable number of arguments to a function, it is mostly used to pass a non-key argument and variable-length argument list.
When dictionaries are iterated as lists the iteration takes the keys of it, for example
for key in tmp:
print(key)
is the same as
for key in tmp.keys():
print(key)
in this case, unpacking as *tmp
is equivalent to *tmp.keys()
, ignoring the values. If you want to use the values you can use *tmp.values()
.
Double asterisk is used for when you define a function with keyword parameters such as
def foo(a, b):
or
def foo(**kwargs):
here you can store the parameters in a dictionary and pass it as **tmp
. In the first case keys must be strings with the names of the parameter defined in the function firm. And in the second case you can work with kwargs
as a dictionary inside the function.
def foo(a,b)
return a+b
tmp = {1:2,3:4}
foo(*tmp) #you get 4
foo(**tmp)
In this case:foo(*tmp)
mean foo(1, 3)
foo(**tmp)
mean foo(1=2, 3=4)
, which will raise an error since 1
can't be an argument. Arg must be strings and (thanks @ Alexander Reynolds for pointing this out) must start with underscore or alphabetical character. An argument must be a valid Python identifier. This mean you can't even do something like this:
def foo(1=2, 3=4):
<your code>
or
def foo('1'=2, '3'=4):
<your code>
See python_basic_syntax for more details.
It is a Extended Iterable Unpacking.
>>> def add(a=0, b=0):
... return a + b
...
>>> d = {'a': 2, 'b': 3}
>>> add(**d)#corresponding to add(a=2,b=3)
5
For single *,
def add(a=0, b=0):
... return a + b
...
>>> d = {'a': 2, 'b': 3}
>>> add(*d)#corresponding to add(a='a',b='b')
ab
Learn more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With