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
  • 3,994
  • 2
  • 20
  • 39
FI-Info
  • 515
  • 5
  • 14

1 Answers1

19

Below (the code below return 10)

func.__closure__[0].cell_contents
balderman
  • 21,028
  • 6
  • 30
  • 43