closures

Dictionary comprehension with lambda expression fails to produce desired result

Dictionary comprehension with lambda expression fails to produce desired result Question: I’m creating a one liner to map the string of int to a function testing if the values are matched. Ideally, the result dictionary d behaves like d[‘0’](0) is True and d[‘0’](1) is False. But instead, I get the following output: >>> d = …

Total answers: 2

How to understand closure in a lambda?

How to understand closure in a lambda? Question: I want to make 5 buttons in a loop, and for each buttons bind a commend to print the index. In the following solution it always prints the same index. My code like this: for i in range(5): make_button = Tkinter.Button(frame, text =”make!”, command= lambda: makeId(i)) def …

Total answers: 1

Cell-var-from-loop warning from Pylint

Cell-var-from-loop warning from Pylint Question: For the following code: for sort_key, order in query_data[‘sort’]: results.sort(key=lambda k: get_from_dot_path(k, sort_key), reverse=(order == -1)) Pylint reported an error: Cell variable sort_key defined in loop (cell-var-from-loop) Could anyone give a hint what is happening here? From pylint source code the description is: A variable used in a closure is …

Total answers: 2

How can "NameError: free variable 'var' referenced before assignment in enclosing scope" occur in real code?

How can "NameError: free variable 'var' referenced before assignment in enclosing scope" occur in real code? Question: While I was hanging out in the Python chatroom, someone dropped in and reported the following exception: NameError: free variable ‘var’ referenced before assignment in enclosing scope I’d never seen that error message before, and the user provided …

Total answers: 2

Why can't Python increment variable in closure?

Why can't Python increment variable in closure? Question: In this code snippet I can print the value of counter from inside the bar function def foo(): counter = 1 def bar(): print(“bar”, counter) return bar bar = foo() bar() But when I try to increment counter from inside the bar function I get an UnboundLocalError …

Total answers: 3

Is it beneficial to replace a simple Python class with a closure?

Is it beneficial to replace a simple Python class with a closure? Question: I have the following Python class: class class A: “””a class that increments internal variable””” def __init__(self, x): self._x = x def incr(self): self._x = (self._x + 1) % 10 return self._x I heard a talk that recommended that such classes with …

Total answers: 3

Python closure vs javascript closure

Python closure vs javascript closure Question: The following closure function works fine in javascript. function generateNextNumber(startNumber) { var current = startNumber; return function(){ return current += 1; } } var getNextNumber = generateNextNumber(10); for (var i = 0; i < 10; i++) { console.log(getNextNumber()); } I tried to do the same in Python def generateNextNumber(startNumber): …

Total answers: 2

How to inject variable into scope with a decorator?

How to inject variable into scope with a decorator? Question: [Disclaimer: there may be more pythonic ways of doing what I want to do, but I want to know how python’s scoping works here] I’m trying to find a way to make a decorator that does something like injecting a name into the scope of …

Total answers: 11

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