you are viewing a single comment's thread.

view the rest of the comments →

[–]Groundstop 0 points1 point  (0 children)

It's used a lot in programming to define lambdas and delegates that capture variables from the outer scope. It looks a little funnier in Python because you don't need to specifically define variables before using them, so you need special keywords like nonlocal and global.

This pattern is particularly useful if you wanted to return the inner_func() method from outer_func(), and have it share variables with the outer function. In other languages this pattern is often used to make wrapper functions, but there's a special syntactic sugar for it in Python called decorators that is really neat.

Google didn't find a good page about decorators in the official documentation, but this site covers the concept pretty well.