Jun-25-2021, 08:56 AM
Check out this script:
What I don’t understand about the above code snippet is how (or why) Python manages to return a value at line 3 for example when no
Only at line 15 is there a string passed in as the
For my future reference, I encountered this code snippet while reading Dan Bader’s Real Python book in the section: “3.1 Python’s Functions Are First-Class”.
def get_speak_func(volume): def whisper(text): return text.lower() + '...' def yell(text): return text.upper() + '!' if volume > 0.5: return yell else: return whisper print(get_speak_func(0.3)) print(get_speak_func(0.7)) speak_func = get_speak_func(0.7) print(speak_func) print(speak_func('Hello'))Here is the output:
Output:<function get_speak_func.<locals>.whisper at 0x7ff195cb9e50>
<function get_speak_func.<locals>.yell at 0x7ff195cb9ee0>
<function get_speak_func.<locals>.yell at 0x7ff195cb9e50>
HELLO!
In the above code snippet, what I do understand is this: the get_speak_func()
takes in a parameter on a scale of 0.0
to 1.0
which is a measure of volume. Nested within that function are two lesser functions which take in some text and either return the text as lower case or upper case. If the volume parameter is 0.5
or greater, then under this condition, the yell()
function is triggered and the text entered is returned to the parent function as upper case. In all other cases, the whisper()
function is triggered and returned. One advantage that this function offers is that it accepts a behaviour as an argument but also returns a behaviour.What I don’t understand about the above code snippet is how (or why) Python manages to return a value at line 3 for example when no
text
data is passed in. When get_speak_func()
is called at line 11 with 0.3
as a Float value when volume
is passed in, Python proceeds to the next line where the whisper()
function is defined taking in a text
string variable which is not declared anywhere. In this situation, my initial inclination is to expect Python to throw ValueError because no text is passed in. Yet as you can see in the output, the function is proven to exist at a location in memory (as it appears in the output). Only at line 15 is there a string passed in as the
text
variable as input. That makes a little bit more sense to me however what is throwing me off now is this: the original function definition handles two variables volume
and text
but they only work if you call the function two separate times. Python won’t accept them if you pass them in together:print(speak_func(0.1, 'Good Bye')) Traceback (most recent call last): File "/home/<user>/dev/projects/python/2018-and-2020/rpf.py", line 16, in <module> print(get_speak_func(0.1, 'Good Bye')) TypeError: get_speak_func() takes 1 positional argument but 2 were givenWhy does Python require nested functions to be called separately, with a new parameter each time? How does Python do this? Are the inner functions kind of like ‘yielding’ the lower()/upper() transformation methods as Python proceeds to evaluate them with the complete
text
string on the second call?For my future reference, I encountered this code snippet while reading Dan Bader’s Real Python book in the section: “3.1 Python’s Functions Are First-Class”.