467,890 Members | 1,663 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Usage of *args

*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
Mar 18 '20 #1
  • viewed: 4718

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Peter Kleiweg | last post: by
2 posts views Thread by joe | last post: by
8 posts views Thread by Grant Richard | last post: by
7 posts views Thread by Leonel Gayard | last post: by
reply views Thread by Mick | last post: by
20 posts views Thread by =?Utf-8?B?cmtibmFpcg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.