We use two operators * (for tuples) and ** (for dictionaries).
Background
Consider a situation where we have a function that receives four arguments. We want to make call to this function and we have a list of size 4 with us that has all arguments for the function. If we simply pass list to the function, the call doesn’t work.
python - Sample - python code :
python tutorial - Output :
Unpacking
We can us * to unpack the list so that all elements of it can be passed as different parameters.
python - Sample - python code :
python tutorial - Output :
As another example, consider the built-in range() function that expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:
python - Sample - python code :
Packing
When we don’t know how many arguments need to be passed to a python function, we can use Packing to pack all arguments in a tuple.
python - Sample - python code :
python tutorial - Output :
The above function mySum() does ‘packing’ to pack all the arguments that this method call receives into one single variable. Once we have this ‘packed’ variable, we can do things with it that we would with a normal tuple. args[0] and args[1] would give you the first and second argument, respectively. Since our tuples are immutable so if you convert the args tuple to a list you can also modify, delete and re-arrange items in i just like a normal list.
Packing and Unpacking
Below is an example that shows both packing and unpacking.
python - Sample - python code :
python tutorial - Output :
** is used for dictionaries
python - Sample - python code :
python tutorial - Output :
Here ** unpacked the dictionary used with it, and passed the items in the dictionary as keyword arguments to the function. So writing “fun(1, **d)” was equivalent to writing “fun(1, b=8, c=16)”.
python - Sample - python code :
python tutorial - Output :
Applications and Important Points
1. Used in socket programming to send a infinite no. of requests to server.
2. Used in Django framework to send variable arguments to view functions.
3. There are wrapper functions that require us to pass in variable argurents.
4. Modification of arguments become easy but at the same time validation is not proper so they must be used with care.
Wikitechy tutorial site provides you all the learn python , learn python for dummies , python coding classes , python online training course , online python course , python training courses , python programming classes , python classes online , python programming for dummies , online python class
Related Searches to Packing and Unpacking Arguments in Python
python list of tuplesargs pythonpython struct modulepython struct pack examplepython return tuplepython splatpython tuple appendpython print binarypython tuple assignmentpython binary stringpython struct examplepython string to binarypython convert list to tuplelist of tuples pythonpython binarypython print tuplepython convert to binarypython argspython structpython unzipstruct pythonpython bin to hexpython binary to inttuple in pythonpython tuple listpython open binary filepython binary datapython commapython star operatorpython parametersargs kwargs pythonpython tuple methodspython tuple examplehex to binary pythonpython 3 tuplepython binasciipython tuple vs listpython swappython pairpython read binary datapython c structprint binary pythonpython tuple functionstruct in pythonpython tuple operationspython main argstuplas pythonpython parameter passingbinary pythonpython hexlifypython to binarypython named parameterswhat is a tuple in pythonwhat is tuple in pythontuple vs list pythonpython integer to binarypython3 structкортежи pythonhow to create a tuple in pythonpython struct classwhat are tuples in pythonunpacking argument lists pythonpython unpacking tuplepython unpacking listpython unpacking dictionarypython unpack list into variablespython unpacking return valuespacking melonspython 3 unpacking