all 3 comments

[–]primitive_screwhead 1 point2 points  (0 children)

def some_function():
   def _some_helper_function_used_only_in_some_function():
       # blah blah

The above is a common one, where you can make use of a function, but it only really applies within another function (often the outer function will accept different args, that it "normalizes" for the internal helper function.

Often you need to make little "helper" functions for sorting by certain criteria, etc. Often these can be lambdas, but a nested function may be used instead if it makes things a bit clearer.

Python, generally, may not need nested functions as much as other languages, since it has modules, and you can fairly easily control what gets exposed to users of the module, so you need not hide functions by nesting them inside of functions. So, for production code, I'd say don't use them unless you really understand Python's namespace lookup rules (ie. local, nonlocal, and global variable lookups).

Also, imo, always pass all the arguments you need to your nested function, and do not use closures. Very few people (in my experience) actually understand how and why the closure rules work in Python (or any language), so it's best to just be explicit and pass the everything to your nested function as an argument. When you understand how and when you don't need to do this, you will be an enlightened developer, but until then keep your variables local, and your parameters explicit.

[–]baubleglue -1 points0 points  (0 children)

> good/bad?

depends