{"id":40,"date":"2020-01-12T19:57:46","date_gmt":"2020-01-12T19:57:46","guid":{"rendered":"https:\/\/reghif.co.uk\/teach\/?p=40"},"modified":"2020-01-12T19:59:14","modified_gmt":"2020-01-12T19:59:14","slug":"passing-functions-as-arguments-with-arguments","status":"publish","type":"post","link":"https:\/\/reghif.co.uk\/teach\/2020\/01\/12\/passing-functions-as-arguments-with-arguments\/","title":{"rendered":"Passing functions as arguments with arguments"},"content":{"rendered":"\n<p>I appreciate this is a difficult one to describe, but things like Tkinter, PyGame or anything after a callback (calling another function when something happens) can throw up complications when you want to pass an argument.<\/p>\n\n\n\n<p>Consider the following line:<\/p>\n\n\n\n<p><code>threading.Timer(5, bar).start()<\/code><\/p>\n\n\n\n<p>That&#8217;s fairly straight forward that it&#8217;s calling the function <em>bar<\/em> after 5 seconds. However, what happened if you wanted to call <em>bar<\/em> with an argument? Just replacing <em>bar<\/em> with <em>bar(test)<\/em> wouldn&#8217;t work as it would call that function when the line is first encountered, not after 5 seconds. <\/p>\n\n\n\n<p>There are two ways to solve this. Partial and Lambda. The two lines below are very similar in function:<\/p>\n\n\n\n<p><code>threading.Timer(5, partial(foo, myNewVar)).start()<\/code><\/p>\n\n\n\n<p><code>threading.Timer(5, lambda: foo(myNewVar)).start()<\/code><\/p>\n\n\n\n<p>The main difference between the two is when the value of myNewVar is retrieved from memory. In the first example, partial, it&#8217;s retrieved when you call partial. So the value of myNewVar will be the value at the point the timer is started.<\/p>\n\n\n\n<p>The second example, lambda, the value of myNewVar is retrieved when it is called, so if myNewVar changes in the 5 seconds before it&#8217;s run, it will take the new value whereas partial would still take the original value.<\/p>\n\n\n\n<p>This can be explored in the attached Python file.<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/reghif.co.uk\/teach\/wp-content\/uploads\/2020\/01\/partialLambdaTest.py\">partialLambdaTest<\/a><a href=\"https:\/\/reghif.co.uk\/teach\/wp-content\/uploads\/2020\/01\/partialLambdaTest.py\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I appreciate this is a difficult one to describe, but things like Tkinter, PyGame or anything after a callback (calling another function when something happens) can throw up complications when you want to pass an argument. Consider the following line: threading.Timer(5, bar).start() That&#8217;s fairly straight forward that it&#8217;s calling the function bar after 5 seconds. &#8230;.&nbsp;&nbsp;<a class=\" special\" href=\"https:\/\/reghif.co.uk\/teach\/2020\/01\/12\/passing-functions-as-arguments-with-arguments\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[8,10],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-a-level","tag-a-level","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":4,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":45,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/40\/revisions\/45"}],"wp:attachment":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}