Image for post
Image for post
Photo by Greg Rakozy on Unsplash

Closure is when a function is able to remember is lexical scope even when that function is executing outside its lexical scope.

Lets Looks at a Practical Example:


Image for post
Image for post
Photo by Paul Esch-Laurent on Unsplash

Motivation

Here are some examples of common pitfalls:


Image for post
Image for post
Photo by Markus Spiske on Unsplash

Lets simply start by showing some simple examples:

Function Declarations

A function declaration is simply a statement that starts with the word function .

Function Expressions

A function expression is simply a function that does not start with the word function .


Image for post
Image for post
Photo by Paul Esch-Laurent on Unsplash

Lexical Scope — The scope of a name binding — an association of a name to an entity, such as a variable — is the region of a computer program where the binding is valid

Scope

Scope Simply determines the visibility of variables through a certain set of rules which are set in place and change depending on the Language you are programming in — Thats it, not too complicated!

Lexical Scope

Lexical Scope, is a type of scope — Sometimes also referred to as Static Scope. Lexical Scoping simply defines how variable names are resolved in nested functions e.g …


Image for post
Image for post
Photo by Chris Ried on Unsplash

In Computer Programming, the scope of a name binding — an association of a name to an entity, such as a variable — is the region of a computer program where the binding is valid:where the name can be used to refer to the entity. Such a region is referred to as a scope block.

So, the above definition may be a little hard to depict, so lets examine what that means in reference to python and its own Scope

There are four Scopes in Python:

1. Local — Inside the current function 2. Enclosing — Inside enclosing functions 3. Global — At the top level…

Connor Littleton

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store