{"id":2404,"date":"2023-01-31T16:56:58","date_gmt":"2023-01-31T16:56:58","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2404"},"modified":"2023-01-31T16:57:00","modified_gmt":"2023-01-31T16:57:00","slug":"python-multi-args","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/01\/31\/python-multi-args\/","title":{"rendered":"Python: Multi-Args"},"content":{"rendered":"\n<p>In Python, you can define a function with a variable number of arguments using the <code>*<\/code> and <code>**<\/code> syntax in the function definition.<\/p>\n\n\n\n<p>A single asterisk <code>*<\/code> in the function definition is used to capture positional arguments passed to the function as a tuple.<\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def foo(*args):\n    for arg in args:\n        print(arg)\n\nfoo(1, 2, 3)\n# Output:\n# 1\n# 2\n# 3<\/code><\/pre>\n\n\n\n<p>In this example, <code>foo<\/code> is defined to accept a variable number of positional arguments. The single asterisk <code>*<\/code> in the function definition <code>def foo(*args)<\/code> tells Python to capture any positional arguments passed to the function as a tuple <code>args<\/code>. When you call <code>foo(1, 2, 3)<\/code>, the arguments <code>1<\/code>, <code>2<\/code>, and <code>3<\/code> are captured as a tuple <code>(1, 2, 3)<\/code> and passed to the function as <code>args<\/code>.<\/p>\n\n\n\n<p>Double asterisks <code>**<\/code> in the function definition are used to capture keyword arguments passed to the function as a dictionary.<\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def bar(**kwargs):\n    for key, value in kwargs.items():\n        print(f\"{key}: {value}\")\n\nbar(a=1, b=2, c=3)\n# Output:\n# a: 1\n# b: 2\n# c: 3<\/code><\/pre>\n\n\n\n<p>In the example above, <code>bar<\/code> is defined to accept a variable number of keyword arguments. <\/p>\n\n\n\n<p>The double asterisks <code>**<\/code> in the function definition <\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>def bar(**kwargs) <\/code><\/pre>\n\n\n\n<p>tells Python to capture any keyword arguments passed to the function as a dictionary <code>kwargs<\/code>. When you call <code>bar(a=1, b=2, c=3)<\/code>, the keyword arguments <code>a=1<\/code>, <code>b=2<\/code>, and <code>c=3<\/code> are captured as a dictionary <code>{'a': 1, 'b': 2, 'c': 3}<\/code> and passed to the function as <code>kwargs<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, you can define a function with a variable number of arguments using the * and ** syntax in the function definition. A single asterisk * in the function definition is used to capture positional arguments passed to the function as a tuple. Here&#8217;s an example: In this example, foo is defined to accept [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[270],"class_list":["post-2404","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-python","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2404"}],"version-history":[{"count":1,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2404\/revisions"}],"predecessor-version":[{"id":2405,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2404\/revisions\/2405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}