nested-function

Return value from nested function 'not defined'

Return value from nested function 'not defined' Question: I have a nested function that is returning two values. When I try to call those values I get an error saying its not defined. async def wallet(self, interaction:discord.Interaction, button:discord.ui.Button): ### Blah blah blah ### @sync_to_async def get_user_wallet(): private_key = abc wallet = xyz return wallet, private_key …

Total answers: 2

Returning value in a nested function when using memoization

Returning value in a nested function when using memoization Question: I am trying to implement a count variable in the function below using dynamic programming specifically memoization. The method calculates the value in a Fibonacci sequence at a given index. I cannot figure out why the count (in this case the number of times this …

Total answers: 1

Python inner function sets vs nonlocal

Python inner function sets vs nonlocal Question: I believe I know the answer to this, but wanted to double-check because I find this a bit confusing. def outerFunc(): mySet = set() a = 0 def innerFunc(): mySet.add(1) mySet.add(2) a = 7 innerFunc() print(mySet) # {1, 2} print(a) # 0 Here, if I want to change …

Total answers: 1

Function inside function – every time?

Function inside function – every time? Question: Let we have this code: def big_function(): def little_function(): ……. ……… The Python documentation says about def statement: A function definition is an executable statement. Its execution binds the function name… So, the question is: Does def little_function() execute every time when big_function is invoked? Question is about …

Total answers: 2

Local variables in nested functions

Local variables in nested functions Question: Okay, bear with me on this, I know it’s going to look horribly convoluted, but please help me understand what’s happening. from functools import partial class Cage(object): def __init__(self, animal): self.animal = animal def gotimes(do_the_petting): do_the_petting() def get_petters(): for animal in [‘cow’, ‘dog’, ‘cat’]: cage = Cage(animal) def pet_function(): …

Total answers: 4

Why aren't python nested functions called closures?

Why aren't python nested functions called closures? Question: I have seen and used nested functions in Python, and they match the definition of a closure. So why are they called "nested functions" instead of "closures"? Are nested functions not closures because they are not used by the external world? UPDATE: I was reading about closures …

Total answers: 10

How do nested functions work in Python?

How do nested functions work in Python? Question: def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 Why does the nested function remember the first value 2 even though maker() has returned and exited by the time action() is …

Total answers: 9

Nested Function in Python

Nested Function in Python Question: What benefit or implications could we get with Python code like this: class some_class(parent_class): def doOp(self, x, y): def add(x, y): return x + y return add(x, y) I found this in an open-source project, doing something useful inside the nested function, but doing absolutely nothing outside it except calling …

Total answers: 7