Questions tagged [nested-function]

Nested functions are functions whose definition is lexically enclosed inside another function definition. Nested functions are not necessarily available in every language having the concept of function, and the exact meaning of the term varies among languages.

410 questions
464
votes
10 answers

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 global, scope? instead. Although Python 2 is…
ooboo
  • 16,259
  • 13
  • 37
  • 32
279
votes
10 answers

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 they are not used by the external world? UPDATE:…
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
180
votes
12 answers

Is nested function a good approach when required by only one function?

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 method_b(arg): return some_data In Python we…
nukl
  • 10,073
  • 15
  • 42
  • 58
122
votes
8 answers

JavaScript Nested function

I got a piece of code for javascript which I just do not understand: function dmy(d) { function pad2(n) { return (n < 10) ? '0' + n : n; } return pad2(d.getUTCDate()) + '/' + pad2(d.getUTCMonth() + 1) + '/' + …
Thomas
  • 33,544
  • 126
  • 357
  • 626
116
votes
4 answers

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, animal): self.animal = animal def…
noio
  • 5,744
  • 7
  • 44
  • 61
91
votes
12 answers

What are PHP nested functions for?

In JavaScript nested functions are very useful: closures, private methods and what have you.. What are nested PHP functions for? Does anyone use them and what for? Here's a small investigation I did
meouw
  • 41,754
  • 10
  • 52
  • 69
90
votes
7 answers

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) I found this in an open-source project, doing…
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
64
votes
9 answers

Do any languages / compilers utilize the x86 ENTER instruction with a nonzero nesting level?

Those familiar with x86 assembly programming are very used to the typical function prologue / epilogue: push ebp ; Save old frame pointer. mov ebp, esp ; Point frame pointer to top-of-stack. sub esp, [size of local variables] ... mov esp, ebp ;…
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
61
votes
1 answer

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, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> { fn…
Challe
  • 711
  • 3
  • 7
60
votes
9 answers

How do nested functions work in Python?

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…
eozzy
  • 66,048
  • 104
  • 272
  • 428
51
votes
11 answers

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() function outside the initValidation() function? I've…
Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
32
votes
1 answer

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 direct calls of nested functions by passing a context…
fuz
  • 88,405
  • 25
  • 200
  • 352
32
votes
2 answers

Is self captured within a nested function

With closures I usually append [weak self] onto my capture list and then do a null check on self: func myInstanceMethod() { let myClosure = { [weak self] (result : Bool) in if let this = self { …
29
votes
2 answers

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 statement. Its execution binds the function…
dondublon
  • 701
  • 1
  • 6
  • 13
28
votes
8 answers

Simulating nested functions in C++

In C the following code works in gcc. int foo( int foo_var ) { /*code*/ int bar( int bar_var ) { /*code*/ return bar_var; } return bar(foo_var); } How can I achieve the same functionality of nested functions in C++ with the gcc…
Paul McCutcheon
  • 283
  • 1
  • 3
  • 4
1
2 3
27 28