<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>python on Coffee and Contemplation</title><link>https://coffeeandcontemplation.dev/tags/python/</link><description>Recent content in python on Coffee and Contemplation</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><managingEditor>dev@gmail.com (dev)</managingEditor><webMaster>dev@gmail.com (dev)</webMaster><lastBuildDate>Sat, 31 Oct 2020 00:00:00 +0000</lastBuildDate><atom:link href="https://coffeeandcontemplation.dev/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>Context managers in python</title><link>https://coffeeandcontemplation.dev/post/2020-10-31-python-context-manager/</link><pubDate>Sat, 31 Oct 2020 00:00:00 +0000</pubDate><author>dev@gmail.com (dev)</author><guid>https://coffeeandcontemplation.dev/post/2020-10-31-python-context-manager/</guid><description>Say, you have to open and process a file in python. You can do that using a function like this
def open_and_process(filename): file = open(filename) process(file) file.close() # This is important If an exception occurs in the process function, file.close() won&amp;rsquo;t be executed.
This pattern is the same for other types of resources too:
Acquire resource Use resource Release resource If something fails during the use phase, the resource won&amp;rsquo;t be released cleanly.</description></item><item><title>Higher order functions and where to find them - Decorators</title><link>https://coffeeandcontemplation.dev/post/2020-10-23-higher-order-functions-3/</link><pubDate>Fri, 23 Oct 2020 00:00:00 +0000</pubDate><author>dev@gmail.com (dev)</author><guid>https://coffeeandcontemplation.dev/post/2020-10-23-higher-order-functions-3/</guid><description>We saw what higher order functions are and how they can be used in creating partially applied functions in previous posts. Now let us see how we can use higher order functions to implement python decorators.
Let us say you want to intercept your function call to log the arguments passed and the return value. ie.
def add(a, b): return a + b add(2, 3) # When called with 2 and 3 as arguments, it should print &amp;gt; Function add called with arguments: 2, 3 &amp;gt; Function add returned: 5 It is quite easy to do this.</description></item><item><title>Higher order functions and where to find them - Partial Application</title><link>https://coffeeandcontemplation.dev/post/2020-10-22-higher-order-functions-2/</link><pubDate>Thu, 22 Oct 2020 00:00:00 +0000</pubDate><author>dev@gmail.com (dev)</author><guid>https://coffeeandcontemplation.dev/post/2020-10-22-higher-order-functions-2/</guid><description>In the previous post, we discussed higher order functions. Ones that take functions as arguments and ones that return functions as arguments. Now let us look at some that do both.
In languages like haskell, there is a feature called Currying. When a function which takes multiple arguments is called with less than the number of expected arguments, it returns another function with the arguments &amp;ldquo;partially applied&amp;rdquo;1. For Eg:-
-- Define a function add a b = a + b -- Call the function.</description></item><item><title>Higher order functions and where to find them</title><link>https://coffeeandcontemplation.dev/post/2020-10-21-higher-order-functions-1/</link><pubDate>Wed, 21 Oct 2020 00:00:00 +0000</pubDate><author>dev@gmail.com (dev)</author><guid>https://coffeeandcontemplation.dev/post/2020-10-21-higher-order-functions-1/</guid><description>My first encounter with a higher order function was when I ran man qsort and noticed its type.
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); For someone who had only seen primitive types like int and long and char, int (*compar)(const void *, const void *) was quite the surprise. I later found out that it was the type for a function which took two arguments and returned an int.</description></item></channel></rss>