Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access closed over variables given only the closure function? [duplicate]

In the following example:

def speak(volume):
    def whisper(text):
        print(text.lower() + ('.' * volume))
    def yell(text):
        print (text.upper() + ('!' * volume))
    if volume > 1:
        return yell
    elif volume <= 1:
        return whisper


func = speak(volume=10)
func('hello')
HELLO!!!!!!!!!! # <== obviously `10` is stored in `func` somewhere

Given func, how would I get the "volume"? Is there something within the func namespace which gives the value of 10? I thought perhaps it would be in func.__globals__ or func.__dict__ but it's in neither.

like image 902
FI-Info Avatar asked Oct 19 '19 22:10

FI-Info


People also ask

What is a closed over variable?

The term "closed-over" variable in that post means that the variable is not accessible by the caller of the function; it is neither seen nor modifiable from the context where the function is being called.

What are closures few common uses for closures?

Closures are frequently used in JavaScript for object data privacy, in event handlers and callback functions, and in partial applications, currying, and other functional programming patterns.

Which variable is deleted when JavaScript page is closed?

Global variables live until the page is discarded, like when you navigate to another page or close the window. Local variables have short lives. They are created when the function is invoked, and deleted when the function is finished.

How does function create a closure?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

What is the difference between a closure and a private variable?

It makes it possible for a function to have " private " variables. The counter is protected by the scope of the anonymous function, and can only be changed using the add function. A closure is a function having access to the parent scope, even after the parent function has closed.

What are closures in functional programming?

Closures in functional programming are the functions that are aware of their surroundings By this, I mean that a closure function has access to the variables and parameters defined in the outer scope.

What is closure in JavaScript?

In JavaScript, Closure means that an inner function always has access to the variables and parameters of its outer function, even after the outer function has returned. The closure is created when an inner function has access to its outer function variables and arguments. The inner function has access to –

What is the difference between a closure and an outer function?

The local variable text of the outer function is a non-local variable for the inner function which it can access but not modify. A closure is a function object (a function that behaves like an object) that remembers values in enclosing scopes even if they are not present in memory.


Video Answer


1 Answers

Below (the code below return 10)

func.__closure__[0].cell_contents
like image 144
balderman Avatar answered Oct 09 '22 11:10

balderman