Dec-17-2020, 10:59 PM
(This post was last modified: Dec-17-2020, 10:59 PM by Pedroski55.)
Thanks!
A little further in my book, Reuven Lerner notes:
Then he gives this example of "function in function":
A little further in my book, Reuven Lerner notes:
Quote:Working with inner functions and closures can be quite surprising and
confusing at first. That’s particularly true because our instinct is to believe
that when a function returns, its local variables and state all go away. Indeed,
that’s normally true—but remember that in Python, an object isn’t released
and garbage-collected if there’s at least one reference to it. And if the inner
function is still referring to the stack frame in which it was defined, then the
outer function will stick around as long as the inner function exists.
Then he gives this example of "function in function":
#! /usr/bin/python3 # password generator import random def create_password_generator(characters): def create_password(length): output = [] for i in range(length): output.append(random.choice(characters)) return ''.join(output) return create_password alpha_password = create_password_generator('abcdef') symbol_password = create_password_generator('!@#$%') print(alpha_password(5)) print(alpha_password(10)) print(symbol_password(5)) print(symbol_password(10))Things become clearer!