4 Replies - 739 Views - Last Post: 18 February 2017 - 03:28 AM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

A few questions about nested functions

Posted 17 February 2017 - 01:37 AM

Ive been on every website and viewed videos about it in different languages (I couldn't understand what was said in the foreign language, but I wanted to see if I could visually understand what was said). I get that nested functions are similar to using class, but they have less lines of code. I think I understand more or less how they work with the examples and the way I understood it was this way. The first outside function is available for everyone to see, the inside function isn't. At least that's the way I understood it. And please correct me if I'm wrong.)

def outside(n):
	def inside(z):
		return n+z
	return inside

a=outside(2)
a(2)
4



Sorry if I sound arrogant or condescending but I can't see any real world reason why I would use nested functions. Every example I found online was similar to the one I just showed above. And I don't see how or when I would write a code like that. Can someone please show me an example when they used a nested function for something practical, say a game they wrote or a program they used where they used it?. I learn and understand things better with visual examples.

Is This A Good Question/Topic? 0
  • +

Replies To: A few questions about nested functions

#2 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,765
  • Joined: 27-December 13

Re: A few questions about nested functions

Posted 17 February 2017 - 02:01 AM

Ask yourself why you use functions in the first place.
Probably to simplify otherwise repeating code, or to give your code a better structure.

If, however, the function you build is itself repetitive or complex; then it may make sense to break down this 'first' function into smaller 'sub-functions' i.e. nested functions.

I have made a game, where upon winning a circle appears flashing a few times.
The winning situation is handled in a function 'winning()', and inside I have a support-function, 'blink()'.
The blink() is nested inside winning() - to keep the scope clear.

You can always just use normal functions, the benefit from nested functions are primarily better structure (it is clear where the nested function is used) and clarity of scope.

Try take a look on my tutorial: http://www.dreaminco...-and-nonlocals/
Was This Post Helpful? 1
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

Re: A few questions about nested functions

Posted 17 February 2017 - 02:13 PM

That was an amazing tutorial and you actually answered some of the other questions I was investigating (globals). I do have a question though, I read and I wish I saved the website a person mentioned that in doing nested functions you're suppose to do a closure. But from what I understood from your tutorial, you automatically do that when you make a nested function and you have the inner function. Did I understand that correctly?.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: A few questions about nested functions

Posted 17 February 2017 - 02:22 PM

I have moved your post to your original topic rather than attaching it to the tutorial.

You can visit your own profile to discover your topics and posts, or looking in the Python forum would have discovered it. Edited: I misread, you are referring to some external page that you found.

There is a helpful discussion here:

Why aren't python nested functions called closures?

It is not a question of a nested function needing to be a closure, it depends on the context and utility of the function.
Was This Post Helpful? 2
  • +
  • -

#5 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,765
  • Joined: 27-December 13

Re: A few questions about nested functions

Posted 18 February 2017 - 03:28 AM

I think the link provided by andrewsw is helpful in order to understand closures vs nested functions.
As autodidact, I do not have sufficient theoretical background to comment further on this subject.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1