{"id":5746,"date":"2017-12-31T22:04:46","date_gmt":"2017-12-31T22:04:46","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5746"},"modified":"2018-01-01T11:31:47","modified_gmt":"2018-01-01T11:31:47","slug":"variable-arguments-in-python","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/variable-arguments-in-python\/","title":{"rendered":"Variable arguments in Python"},"content":{"rendered":"<p>When looking at some built-in functions within builtins.py, I came across the strange syntax<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef ascii(*args, **kwargs): \r\n<\/pre>\n<p>The <em>*args<\/em> and <em>**kwargs<\/em> has a special meaning within Python. The <em>*args<\/em> will give all function parameters as a tuple and the <em>**kwargs<\/em> will give a dictionary of key\/values.<\/p>\n<p><em>Note: convention seems to suggest the parameter names args and kwargs, but these are not enforced naming conventions.<\/em><\/p>\n<p>Let&#8217;s see these two (separately) in action<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef show_args(*args):\r\n    for a in args:\r\n        print(a)\r\n\r\n\r\ndef show_key_values(**kwargs):\r\n    for a in kwargs:\r\n        print(a, kwargs&#x5B;a])\r\n\r\n\r\nshow_args(&quot;Hello&quot;, &quot;World&quot;, 123)\r\nshow_key_values(first=&quot;Hello&quot;, second=&quot;World&quot;, third=123)\r\n<\/pre>\n<p>As you can see, in the <em>show_args<\/em> function we will list each argument out, hence the output to stdout will be<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nHello\r\nWorld\r\n123\r\n<\/pre>\n<p>The <em>show_key_values<\/em> expects key\/value input, hence the field\/property name followed by = and then the value. This will output<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nfirst Hello\r\nsecond World\r\nthird 123\r\n<\/pre>\n<p>In both cases we can also pass no arguments to these methods and nothing will be output, but it&#8217;s also not unusual (as can be seen by the <em>ascii<\/em> function at the start of this post) for the two pieces of syntax to be combined, i.e.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef show_args_key_values(*args, **kwargs):\r\n    for a in args:\r\n        print(a)\r\n\r\n    for a in kwargs:\r\n        print(a, kwargs&#x5B;a])\r\n<\/pre>\n<p>and now this can be called in the following ways<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nshow_args_key_values(&quot;Hello&quot;, &quot;World&quot;, 123, first=&quot;Hello Again&quot;)\r\n<\/pre>\n<p>As you can probably tell from this syntax, you will need to list the list of arguments before the key\/value list of arguments, but you can write the following<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nshow_args_key_values(&quot;Hello&quot;)\r\nshow_args_key_values(first=&quot;World&quot;)\r\n<\/pre>\n<p>and therefore handle either a list of key\/value set of input.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When looking at some built-in functions within builtins.py, I came across the strange syntax def ascii(*args, **kwargs): The *args and **kwargs has a special meaning within Python. The *args will give all function parameters as a tuple and the **kwargs will give a dictionary of key\/values. Note: convention seems to suggest the parameter names args [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[195],"tags":[],"class_list":["post-5746","post","type-post","status-publish","format-standard","hentry","category-python"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5746"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5746\/revisions"}],"predecessor-version":[{"id":5753,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5746\/revisions\/5753"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}