Have you ever found yourself looking at someone else's function and wondered what the argument *args, **kw, or **kwargs means / does?
Example:
def my_function(proto, *args, **kw):
# rest of the function code hereThose arguments are called "Keyword Arguments". Essentially, they are place holders for multiple arguments, and they are extremely useful especially when you need to pass a different number of arguments each time you call the function.
Example:
>> my_function('filter', 'python', 'html', start='now')
>> my_function('proto', 'filter')
>> my_function('ask', pg='2')args, kw, and kwargs themselves aren't "special", it's the asterisks (*) that makes it "special". One * will create a tuple out of all the one off arguments (e.g. 'filter'), where as the double ** will create a dictionary out of all the arguments that have an equals (e.g. start = 'now').
note: If you're using both the single (*) and double (**) asterisk in your function, the single (*) MUST come before the double.
A Simple Test
def kwargs(basic, *huh, **no_way):
print basic, huh, no_way
>> kwargs('proto', 'filter', '.com', a='pythonic', maybe='yes')
>> proto ('filter', '.com') {'a': 'pythonic', 'maybe': 'yes'}
>> kwargs('girls', yes='no')
>> girls () {'yes': 'no'}
So, if you don't want your functions to raise an TypeError or you need your functions to accept variable number of arguments, you can use "Keyword Arguments".


Comments
Variable
January 4, 2011 - 3:43pm — Meat Slicer (not verified)Tried using code but still raising TypeError in function.Maybe it has to do with Astrix.Maybe it has nothing to do with variable arguments.
Great help
August 16, 2011 - 1:36pm — pythonic guest (not verified)Thanks for the description. It really turned on the light regarding the *args **kwargs concepts.
need help
November 27, 2011 - 9:37pm — pythonic guest (not verified)Hello,
this is G.Varatharaju, when i am running autodock, it shows the following error..
plz. help me to solve this problem..
IDLE 1.2.2 ==== No Subprocess ====
>>> No module named UTpackages.UTvolrend.UTVolumeLibrary
Cannot import grid3DCommands. Disabling grid3DCommands...
skipping the isocontour-dependent commands
skipping the isocontour-dependent commands
skipping the isocontour-dependent commands
skipping the isocontour-dependent commands
ERROR *********************************************
Traceback (most recent call last):
File "C:\Program Files\MGLTools 1.5.4\MGLToolsPckgs\ViewerFramework\VF.py", line 735, in tryto
result = apply( command, args, kw )
File "C:\Program Files\MGLTools 1.5.4\MGLToolsPckgs\Pmv\editCommands.py", line 3490, in doit
chs = Numeric.sum(item.atoms.charge)
File "C:\Program Files\MGLTools 1.5.4\MGLToolsPckgs\MolKit\molecule.py", line 116, in __getattr__
res.append(a._charges[a.chargeSet])
KeyError: None
ERROR *********************************************
Traceback (most recent call last):
File "C:\Program Files\MGLTools 1.5.4\MGLToolsPckgs\ViewerFramework\VF.py", line 735, in tryto
result = apply( command, args, kw )
File "C:\Program Files\MGLTools 1.5.4\MGLToolsPckgs\Pmv\editCommands.py", line 3214, in doit
totalCharge, resSet = self.vf.checkResCharges(nodes)
ValueError: too many values to unpack
Cheers By
G.Varatharaju..
AP/Biotech,
GCT, Cbe-13
Tamil nadu
Thanks for the help now *args
April 4, 2012 - 6:33am — Jin Micro (not verified)Thanks for the help now *args and **kwargs is clearer now.
-Forex Contest
Post new comment