{"id":5686,"date":"2023-10-08T18:51:03","date_gmt":"2023-10-08T13:21:03","guid":{"rendered":"https:\/\/nolowiz.com\/?p=5686"},"modified":"2023-10-08T18:51:16","modified_gmt":"2023-10-08T13:21:16","slug":"partial-functions-in-python","status":"publish","type":"post","link":"https:\/\/nolowiz.com\/partial-functions-in-python\/","title":{"rendered":"Partial Functions in Python"},"content":{"rendered":"\n<p>In this tutorial, we will learn partial functions and their usage in Python.<\/p>\n\n\n\n<h2>What is Partial function?<\/h2>\n\n\n\n<p>A partial function in programming is a function that is defined for only a subset of possible inputs. It allows you to specify a fixed number of arguments in advance, leaving some arguments to be provided later. This can be useful for creating reusable and flexible code.  <\/p>\n\n\n\n<p>In Python, we can use the functools&#8217; <a href=\"https:\/\/docs.python.org\/3\/library\/functools.html#functools.partial\" target=\"_blank\" rel=\"noreferrer noopener\">partial function<\/a> to implement the partial function.The  functools is a module in Python&#8217;s standard library that provides higher-order functions and operations on callable objects.<\/p>\n\n\n\n<h3>Example 1: <\/h3>\n\n\n\n<p>Let&#8217;s understand with an example, suppose we have a function to multiply two numbers.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef multiply(first_num, second_num):\n  '''Multiply two numbers'''\n  return first_num * second_num\n<\/pre><\/div>\n\n\n<p>We can convert the multiply function to calculate the double of a number using partial function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom functools import partial\n\ndouble = partial(multiply,second_num=2)\n<\/pre><\/div>\n\n\n<p>Here one argument to multiply function is fixed(2), the partial function double can accept one number. Now we can call the function double :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(double(52))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>104<\/code><\/pre>\n\n\n\n<p>We can also add <a href=\"https:\/\/nolowiz.com\/python-docstrings-format-and-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\">docstring<\/a> to the partial function using the __doc__ dunder function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom functools import partial\n\ndouble = partial(multiply,second_num=2)\ndouble.__doc__ = &quot;Calcualte double of the number&quot;\n<\/pre><\/div>\n\n\n<h3>Example 2:<\/h3>\n\n\n\n<p>Suppose we have a function to calculate the power of a number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef power(number,exponent):\n  return number ** exponent\n<\/pre><\/div>\n\n\n<p>We can define a square and cube partial functions as shown below :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsquare = partial(power,exponent=2)\ncube = partial(power,exponent=3)\n<\/pre><\/div>\n\n\n<p>We can call cube partial function :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(cube(5))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>125<\/code><\/pre>\n\n\n\n<h2>When to use Partial functions<\/h2>\n\n\n\n<p>Partial functions are usable in the following contexts :<\/p>\n\n\n\n<ul><li>When you need to fix one or more arguments of a function and create a new function with reduced arity(The term &#8220;arity&#8221; refers to the number of arguments or parameters that a function or operation can accept).<\/li><li>We want to create a function with default arguments.<\/li><li>When you need to pass a function as an argument to another function with specific arguments already set.<\/li><li>When we want reusable code<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn partial functions and their usage in Python. What is Partial function? A partial function in programming is a function that is defined for only a subset of possible inputs. It allows you to specify a fixed number of arguments in advance, leaving some arguments to be provided later. This &#8230; <a title=\"Partial Functions in Python\" class=\"read-more\" href=\"https:\/\/nolowiz.com\/partial-functions-in-python\/\" aria-label=\"More on Partial Functions in Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Partial Functions in Python - NoloWiz<\/title>\n<meta name=\"description\" content=\"this tutorial, we will learn partial functions and their usage in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nolowiz.com\/partial-functions-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Partial Functions in Python - NoloWiz\" \/>\n<meta property=\"og:description\" content=\"this tutorial, we will learn partial functions and their usage in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nolowiz.com\/partial-functions-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"NoloWiz\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-08T13:21:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-08T13:21:16+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rupesh Sreeraman\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/nolowiz.com\/#organization\",\"name\":\"NoloWiz\",\"url\":\"https:\/\/nolowiz.com\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/nolowiz.com\/#logo\",\"inLanguage\":\"en\",\"url\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png\",\"contentUrl\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png\",\"width\":512,\"height\":512,\"caption\":\"NoloWiz\"},\"image\":{\"@id\":\"https:\/\/nolowiz.com\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/nolowiz.com\/#website\",\"url\":\"https:\/\/nolowiz.com\/\",\"name\":\"NoloWiz\",\"description\":\"Technology news, tips and tutorials\",\"publisher\":{\"@id\":\"https:\/\/nolowiz.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/nolowiz.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/nolowiz.com\/partial-functions-in-python\/#webpage\",\"url\":\"https:\/\/nolowiz.com\/partial-functions-in-python\/\",\"name\":\"Partial Functions in Python - NoloWiz\",\"isPartOf\":{\"@id\":\"https:\/\/nolowiz.com\/#website\"},\"datePublished\":\"2023-10-08T13:21:03+00:00\",\"dateModified\":\"2023-10-08T13:21:16+00:00\",\"description\":\"this tutorial, we will learn partial functions and their usage in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/nolowiz.com\/partial-functions-in-python\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/nolowiz.com\/partial-functions-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/nolowiz.com\/partial-functions-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/nolowiz.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Partial Functions in Python\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/nolowiz.com\/partial-functions-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/nolowiz.com\/partial-functions-in-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414\"},\"headline\":\"Partial Functions in Python\",\"datePublished\":\"2023-10-08T13:21:03+00:00\",\"dateModified\":\"2023-10-08T13:21:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/nolowiz.com\/partial-functions-in-python\/#webpage\"},\"wordCount\":284,\"publisher\":{\"@id\":\"https:\/\/nolowiz.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414\",\"name\":\"Rupesh Sreeraman\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/nolowiz.com\/#personlogo\",\"inLanguage\":\"en\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g\",\"caption\":\"Rupesh Sreeraman\"},\"sameAs\":[\"http:\/\/nolowiz.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Partial Functions in Python - NoloWiz","description":"this tutorial, we will learn partial functions and their usage in Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nolowiz.com\/partial-functions-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Partial Functions in Python - NoloWiz","og_description":"this tutorial, we will learn partial functions and their usage in Python.","og_url":"https:\/\/nolowiz.com\/partial-functions-in-python\/","og_site_name":"NoloWiz","article_published_time":"2023-10-08T13:21:03+00:00","article_modified_time":"2023-10-08T13:21:16+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rupesh Sreeraman","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/nolowiz.com\/#organization","name":"NoloWiz","url":"https:\/\/nolowiz.com\/","sameAs":[],"logo":{"@type":"ImageObject","@id":"https:\/\/nolowiz.com\/#logo","inLanguage":"en","url":"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png","contentUrl":"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png","width":512,"height":512,"caption":"NoloWiz"},"image":{"@id":"https:\/\/nolowiz.com\/#logo"}},{"@type":"WebSite","@id":"https:\/\/nolowiz.com\/#website","url":"https:\/\/nolowiz.com\/","name":"NoloWiz","description":"Technology news, tips and tutorials","publisher":{"@id":"https:\/\/nolowiz.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nolowiz.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en"},{"@type":"WebPage","@id":"https:\/\/nolowiz.com\/partial-functions-in-python\/#webpage","url":"https:\/\/nolowiz.com\/partial-functions-in-python\/","name":"Partial Functions in Python - NoloWiz","isPartOf":{"@id":"https:\/\/nolowiz.com\/#website"},"datePublished":"2023-10-08T13:21:03+00:00","dateModified":"2023-10-08T13:21:16+00:00","description":"this tutorial, we will learn partial functions and their usage in Python.","breadcrumb":{"@id":"https:\/\/nolowiz.com\/partial-functions-in-python\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nolowiz.com\/partial-functions-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nolowiz.com\/partial-functions-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nolowiz.com\/"},{"@type":"ListItem","position":2,"name":"Partial Functions in Python"}]},{"@type":"Article","@id":"https:\/\/nolowiz.com\/partial-functions-in-python\/#article","isPartOf":{"@id":"https:\/\/nolowiz.com\/partial-functions-in-python\/#webpage"},"author":{"@id":"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414"},"headline":"Partial Functions in Python","datePublished":"2023-10-08T13:21:03+00:00","dateModified":"2023-10-08T13:21:16+00:00","mainEntityOfPage":{"@id":"https:\/\/nolowiz.com\/partial-functions-in-python\/#webpage"},"wordCount":284,"publisher":{"@id":"https:\/\/nolowiz.com\/#organization"},"articleSection":["Python"],"inLanguage":"en"},{"@type":"Person","@id":"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414","name":"Rupesh Sreeraman","image":{"@type":"ImageObject","@id":"https:\/\/nolowiz.com\/#personlogo","inLanguage":"en","url":"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g","caption":"Rupesh Sreeraman"},"sameAs":["http:\/\/nolowiz.com"]}]}},"_links":{"self":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/5686"}],"collection":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/comments?post=5686"}],"version-history":[{"count":17,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/5686\/revisions"}],"predecessor-version":[{"id":5703,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/5686\/revisions\/5703"}],"wp:attachment":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/media?parent=5686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/categories?post=5686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/tags?post=5686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}