Skip to Content
Hands-On Reactive Programming with Python
book

Hands-On Reactive Programming with Python

by Romain Picard
October 2018
Intermediate to advanced
420 pages
10h 26m
English
Packt Publishing
Content preview from Hands-On Reactive Programming with Python

Closures

Closures are another feature that are heavily used in functional programming. A closure is the fact that a function can capture the value of a variable in one of its parent scopes. Let's see what this means in the following example:

def action():    print(value)value = "hello"    action()

First, the action function is declared. This function prints the value of value even though value is neither a parameter nor a local variable. So how can this work? When the action function is being called and the print expression is executed, the interpreter searches for a reference of the value variable from the inner scope, up to the outermost scope. The interpreter searches in this order:

  • Local variables
  • Function arguments
  • Global variables

There ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Programming with Design Patterns

Python Programming with Design Patterns

James W. Cooper

Publisher Resources

ISBN: 9781789138726Supplemental Content