14

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.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
FI-Info
  • 635
  • 7
  • 16

1 Answers1

19

Below (the code below return 10)

func.__closure__[0].cell_contents
balderman
  • 22,927
  • 7
  • 34
  • 52