416 questions
524
votes
11
answers
207k
views
What does "nonlocal" do in Python 3?
What does nonlocal do in Python 3.x?
To close debugging questions where OP needs nonlocal and doesn't realize it, please use Is it possible to modify variable in python that is in outer, but not ...
185
votes
12
answers
277k
views
Is nested function a good approach when required by only one function? [closed]
Let's say that a function A is required only by function B, should A be defined inside B?
Simple example. Two methods, one called from another:
def method_a(arg):
some_data = method_b(arg)
def ...
118
votes
4
answers
16k
views
Local variables in nested functions
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, ...
94
votes
7
answers
99k
views
Nested Function in Python
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)
...
63
votes
1
answer
4k
views
Why does std::fs::write(...) use an inner function?
I'm new to Rust and have been looking through the source code a bit, and found this:
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write<P: AsRef<Path&...
53
votes
11
answers
93k
views
Javascript call nested function
I have the following piece of code:
function initValidation()
{
// irrelevant code here
function validate(_block){
// code here
}
}
Is there any way I can call the validate() ...
34
votes
1
answer
7k
views
Implementation of nested functions
I recently found out that gcc allows the definition of nested function. In my opinion, this is a cool feature, but I wonder how to implement it.
While it is certainly not difficult to implement ...
29
votes
2
answers
1k
views
Function inside function - every time?
Let we have this code:
def big_function():
def little_function():
.......
.........
The Python documentation says about def statement:
A function definition is an executable ...
28
votes
7
answers
170k
views
How can I combine multiple nested Substitute functions in Excel?
I am trying to set up a function to reformat a string that will later be concatenated. An example string would look like this:
Standard_H2_W1_Launch_123x456_S_40K_AB
Though sometimes the "S" doesn't ...
23
votes
11
answers
13k
views
Are nested functions a bad thing in gcc ? [closed]
I know that nested functions are not part of the standard C, but since they're present in gcc (and the fact that gcc is the only compiler i care about), i tend to use them quite often.
Is this a bad ...
16
votes
5
answers
37k
views
Golang returning functions
Can anyone explain why 0's and 1's are printed and not anything else? Thank you!
func makeFunction(name string) func() {
fmt.Println("00000")
return func() {
makeFunction2("abcef")
...
15
votes
1
answer
2k
views
With c# why are 'in' parameters not usable in local functions?
For example,
public int DoSomething(in SomeType something){
int local(){
return something.anInt;
}
return local();
}
Why does the compiler issue an error that the something variable cannot ...
14
votes
2
answers
2k
views
Nesting functions and performance in javascript?
Some of coworkers are saying that nesting functions is bad for performance and I wanted to ask about this.
Lets say I have the function:
function calculateStuff() {
function helper() {
// ...
11
votes
5
answers
9k
views
python: how binding works
I am trying to understand, how exactly variable binding in python works. Let's look at this:
def foo(x):
def bar():
print y
return bar
y = 5
bar = foo(2)
bar()
This prints 5 which ...
11
votes
1
answer
3k
views
Line Profiling inner function with Cython
I've had pretty good success using this answer to profile my Cython code, but it doesn't seem to work properly with nested functions. In this notebook you can see that the profile doesn't appear when ...
9
votes
3
answers
4k
views
What is the benefit of nesting functions (in general/in Swift)
I'm just learning some Swift and I've come across the section that talks about nesting functions:
Functions can be nested. Nested functions have access to variables that were declared in the outer ...
9
votes
1
answer
663
views
Are multiple functions in one .m file nested or local when "end" isn't used
In MATLAB you can have multiple functions in one .m file. There is of course the main function, and then either nested or local functions.
Examples of each function type:
% myfunc.m with local ...
8
votes
4
answers
5k
views
Are nested functions faster than global functions in Python?
I prefer using nested functions instead of methods or global functions in Python whenever possible. So I decided to test their performance because it seams that when you define a function in another ...
8
votes
3
answers
5k
views
Correct way to define handlers in functional React components?
As far as I know, there are three ways to define functions in JavaScript.
1. Declaration
function handleEvent(e) {}
2. Assignment
var handleEvent = function(e) {}
3. Arrow
var handleEvent = (e) =&...
7
votes
5
answers
5k
views
Portable nested functions in C
Is it possible to write portable C code using nested functions/blocks?
I understand that gcc only supports nested functions as an non-standard extension, and clang only supports blocks - but is there ...
7
votes
4
answers
521
views
Variable scope in nested functions
Could someone explain why the following program fails:
def g(f):
for _ in range(10):
f()
def main():
x = 10
def f():
print x
x = x + 1
g(f)
if __name__ == '__main__':
main()
...
6
votes
2
answers
5k
views
helper nested functions in CL
I used to write nested helper functions (which, btw, ocasionally used parameters of outer functions and are recursive) in Haskell like this (loop):
sum a b = let
loop s i = if i > b then s else ...
6
votes
2
answers
8k
views
Passing argument from Parent function to nested function Python
here is my code:
def f(x):
def g(n):
if n < 10:
x = x + 1
g(n + 1)
g(0)
When I evaluate f(0), there would be an error "x referenced before assignment".
...
6
votes
1
answer
426
views
While loop counter increasing "exponentially" in spite of using ++
Background information
I am setting up a function which creates a date array based on a start date and an end date.
The function will receive start and end dates, which have first been formatted ...
5
votes
3
answers
7k
views
How to call a function within a function in Lua?
I have this code:
function test()
function awesome()
print("im awesome!")
end
function notawesome()
print("im not awesome.")
end
function notevenawesome()
...