By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 1,068 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Usage of *args

P: 1
*args and **kwargs are mostly used in function definitions. *args and **kwargs allow you to pass a variable number of arguments to a function. What variable means here is that you do not know beforehand how many arguments can be passed to your function by the user so in this case you use these two keywords. *args is used to send a non-keyworded variable length argument list to the function. Here’s an example to help you get a clear idea:
Expand|Select|Wrap|Line Numbers
  1. def test_var_args(f_arg, *argv):
  2.     print("first normal arg:", f_arg)
  3.     for arg in argv:
  4.         print("another arg through *argv:", arg)
  6. test_var_args('yasoob', 'python', 'eggs', 'test')
This produces the following result:

Expand|Select|Wrap|Line Numbers
  1. first normal arg: yasoob
  2. another arg through *argv: python
  3. another arg through *argv: eggs
  4. another arg through *argv: test
1 Week Ago #1
Share this Article
Share on Google+