Why Python decorators rather than closures?

I still haven't got my head around decorators in Python. I've already started using a lot of closures to do things like customize functions and classes in my coding. Eg. class Node : def...

Efficiently replace all accented characters in a string?

For a poor man's implementation of near-collation-correct sorting on the client side I need a JavaScript function that does efficient single character replacement in a string. Here is what I mean...

Modify bound variables of a closure in Python

Is there any way to modify the bound value of one of the variables inside a closure? Look at the example to understand it better. def foo(): var_a = 2 var_b = 3 def _closure(x): return...

What do lambda function closures capture?

Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code: adders=[None, None, None, None] for i in [0,1,2,3]: ...

nonlocal keyword in Python 2.x

I'm trying to implement a closure in Python 2.6 and I need to access a nonlocal variable but it seems like this keyword is not available in python 2.x. How should one access nonlocal variables in...

Scala - calculate average of SomeObj.double in a List[SomeObj]

I'm on my second evening of scala, and I'm resisting the urge to write things in scala how I used to do them in java and trying to learn all of the idioms. In this case I'm looking to just...

Why aren't python nested functions called closures?

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...

Python private class variables that aren't class variables

When trying to access __variables from a class, the parser assumes the 2 underscores are private relative to the current class. Notice how an unrelated function gets a "private" variable. Is this...

transitive closure python tuples

Does anyone know if there's a python builtin for computing transitive closure of tuples? I have tuples of the form (1,2),(2,3),(3,4) and I'm trying to get (1,2),(2,3),(3,4),(1,3)(2,4) Thanks.

create an button on desktop

I've collected many nice wall papers over years. I know python and c++ (a little MFC eperience). I want to make a programme that can change my wallpapers. I want to operate like this: there...

What can multiprocessing and dill do together?

I would like to use the multiprocessing library in Python. Sadly multiprocessing uses pickle which doesn't support functions with closures, lambdas, or functions in __main__. All three of these...

Why can't Python increment variable in closure?

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...

How to create cartesian product

I have a list of integers, a = [0, ..., n]. I want to generate all possible combinations of k elements from a; i.e., the cartesian product of the a with itself k times. Note that n and k are both...

What is a cell in the context of an interpreter or compiler?

Python code objects have an attribute co_cellvars. The documentation to PyPy's bytecode interpreter often uses the term Cell. Among other langauges, Rust provides a Cell datatype. Googling...

How to comment / document uses of Pylint in‑line options?

Disabling a Pylint check or getting around one of its warnings, should not be without a clear reason. I would like to be able to comment these reasons at the place I'm disabling it; so far,...

Printing out the fibonacci series

I am trying to write a simple Python program. It's supposed to return a a closure that returns successive fibonacci numbers: def fibGen(): n_1 = 0 n_2 = 0 n = 1 def fib(): ...

Idioms in python: closure vs functor vs object

So I am curious about the views of more experienced python programmers on the following style question. Suppose that I am building a function that is going to iterate row by row through a pandas...

How to call multi-level nested function in Python?

I am trying to learn Python and have few doubts with .r.t to multi-level nested functions and function closures in Python. Please help me understand how will this work. Questions from the code...

What does "late binding closures" mean?

I have a back-ground of C++ and trying to learn some python. Whilst i understand virtual functions for C++, I unfortunately do not understand what is meant by late binding of closures in...

Replace pickle in Python multiprocessing lib

I need to execute the code below (simplified version of my real code base in Python 3.5): import multiprocessing def forever(do_something=None): while True: do_something() p =...

Simplistic parallel grid search in Python

Ok, full disclosure, I'm not doing a grid search but the minimum example I could find for what I'm about to ask could (with a grain of salt) be reduced to a grid search (in case you're wondering...

Are primitive / reference types stored on the heap or on the stack?

What determines the storage type in JS? Does it have anything to do with the primitive vs reference types? I'm coming from the C++ / python background. Reading the answers and comments to this...

Can FunctionTestCase methods take in arguments?

I'm using unittest for some testing in Python. I am interested in FunctionTestCase. From the docs, this is the sample use: testcase = unittest.FunctionTestCase(testSomething, ...

Understanding closure scope in Python

This is example from Bratt Slatkin's book def sort_priority(values, group): def helper(x): if x in group: return (0, x) return (1, x) ...

Python closures with generator

def multipliers(): return [lambda x : i * x for i in range(4)] print [m(2) for m in multipliers()] I partially understood(which is dangerous) the reason that i is same for all functions...

Why doesn't this higher-order function pass static type checking in mypy?

I'm trying to get to grips with mypy. As an exercise, I'm trying to figure out the right type annotations for some common higher-order functions. But I don't quite understand why the following...

Where is closure and the non-local variable (free variable) stored in python? How does the bindings in closure work?

I understand that python stores call stack and references on the stack and the value objects on the heap. But how does closure and especially the non-local variables (the variables defined in...

How to make C-function pointer that is a closure over another C-function and a Python function parameter, from Python

I'm working with an interesting puzzle - how to create closures of C functions that reference Python data at runtime? This is a little long, but there's some technically interesting stuff here if...

eBay Marketplace Account Deletion/Closure Notifications

I am trying to start an eBay API in Python and I can't find a single answer as to how to get an API key with eBay's new requirements of "Account Deletion/Closure Notifications." Here's the link:...

Dealing with so-called global variables in Rust

We all know that using global variables can lead to subtle bugs. I need to migrate Python programs to Rust, keeping the algorithm intact as far as possible. Once I have demonstrated Python-Rust...