Posts

Showing posts from July, 2022

Python Closure

In these days of high inflation and high interest rates, I am sharing if you a simple Python Closure to calculate compound interest. According to Wikipedia, "a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions". Closure is an elegant style that takes the global namespace of your code clean. It behaves like a class/object instantiation, but in a much simpler way. I will use the class/object language to refer to the closure itself and its elements always in quotes, even if I know that it is not syntactically correct, just for the sake of ease of understanding. Compound Interest Our closure will calculate the compound interest for an initial value (principal) during a period of time and a fixed interest rate that you can set, based on the market conditions. To create a closure the following criterias have to be met: Outer function and an inner function (