There are a lot of good reasons. Personally, I often use nested functions to keep the namespace clean. It's especially useful within object methods :
class Foo(object):
def bar(self):
def baz(val):
return val
return [ baz(i) for i in range(1,101) ]
If I declare baz outside of bar, I either need to make it a method of Foo, or expose it to the entire package.
Today on a PR I saw several functions defined within an str function, and I noted that that should be changed to meet best practices/pep8.
The submitter argued that it wasn't an issue, and sure enough I checked the pep8 and couldn't find a reference to it.
I could've sworn that nesting function declarations is looked down upon/bad practice in Python; am I right or am I just misremembering?
There are a lot of good reasons. Personally, I often use nested functions to keep the namespace clean. It's especially useful within object methods :
class Foo(object):
def bar(self):
def baz(val):
return val
return [ baz(i) for i in range(1,101) ]
If I declare baz outside of bar, I either need to make it a method of Foo, or expose it to the entire package.
I use a nested function to find matches from a list:
def get_exact(data, key, match):
def is_match(item):
if (key in item) and (item[key].lower() == match.lower()):
return item
return False
return [i for i in data if is_match(i)]
No other calls in the project have a need to use is_match(item), so why declare it separately?
However, I will say that, for my example, declaring is_match() outside of get_exact() does run around ~0.04 seconds faster in 10,000 iterations.
def is_match(item, key, match):
if (key in item) and (item[key].lower() == match.lower()):
return item
return False
def get_exact(data, key, match):
return [i for i in data if is_match(i, key, match)]
This really depends on how much nesting you use. After all, you are allowed to use function results directly in expressions to improve readability. Both, code that does not use nested expressions (like assembler code), and code that uses too much nested expressions is hard to read. Good code tries to strike a balance in between the extremes.
So lets look at some examples. The one you gave in your question seems quite legit to me, so nothing to worry here. However, a line like
foo(bar(baz(moo, fab), bim(bam(ext, rel, woot, baz(moo, fab)), noob), bom, zak(bif)));
would definitely not be tolerable. Likewise, code like
double xsquare = x*x;
double ysquare = y*y;
double zsquare = z*z;
double xysquare = xsquare + ysquare;
double xyzsquare = xysquare + zsquare;
double length = sqrt(xyzsquare);
would not be very readable as well. sqrt(x*x + y*y + z*z) is much easier to understand, even though it combines a total of six different operation in one expression.
My advice is to pay attention to what expressions you can still parse in your head easily. The moment you need to take a second look to grasp what a single expression does, it's time to introduce an additional variable.
The concept underlying your question is so important I feel it needs another answer rather than just a comment (as I had started to do).
The other 3 answers thus far provide some useful points of consideration on whether a given situation merits using what you call "nested function calls". But perhaps a more important point is hidden in the comments under your question: in case you missed the subtlety in what those erudite folks are suggesting, Carl, you have discovered for yourself the topic actually called functional programming. If you have never seen the term, you might not have thought it was really a "thing" in @HighPerformanceMark's comment.
But indeed it is! Functional programming has been written about for decades, since John Hughes' seminal paper Why Functional Programming Matters. There are some languages that are functional languages (i.e. they only let you write in a functional programming style), languages like Erlang, Lisp, OCaml, or Haskell. But there are many more languages that are hybrid imperative/functional languages. That is, they are traditionally imperative languages but offer some support for functional programming as well, including Perl, C++, Java, C#, and many more. Wikipedia's entry on functional programming provides a nice section showing a comparison of functional style vs. imperative style for a number of languages.
There is much to say on the differences between imperative and functional styles, but the key starting point is that with functional programming, functions or methods have no side effects, making it in general easier to both understand and debug programs.
For further reading, you might also take a look at Reginald Braithwaite's Why "Why Functional Programming Matters" Matters and another interesting post here on SO, Why functional languages?
I may be calling this wrong, but all I mean is:
def func1():
def func2():I've seen posts explaining when you should use this, but I haven't found much against using it. I seem to be unintentionally using quite a few.
Is using a nested function best practice? Is it 'efficient'?
I only ask as my code seems to be running a little slow at the beginning, so trying to see if I can improve that
So, I am writing a main function (so that I can say if name == "main" because that is good practice). And my main file accesses a lot of classes that are not inside of the main function. This is all fine, but then there is a function in a number of different classes that I want to call at once. Think of it like: reset_box(), reset_circle(), reset_triangle() or whatever.
Under certain conditions I want to just hit all these at once. So, it would be nice to have a function I can call that just does them all at once rather than writing them all out each time.
It would be easiest to do this in the main function, to just add in another function that resets everything. Or is this bad code? Should I take the function out of main and call it that way? The downside of this is that I have to pass it every object that I want to reset which will make it look a little ugly?
What are people's thoughts? Is it better to have a little function inside main that I can call to reset a bunch of objects. Or does it seem better to have the function out of main and just pass in every object that I need to?
I have been writing a class which resembles a binary tree and have been looking to clean up my code a bit and found my way to nested functions. I understand the basic logic behind them and what not, my question is more on how they are used within custom objects.
For example, if i am trying to print the tree in-order and i do something like:
def print_inorder(self):
def display(node, level):
if not node:
return
display(node.left_child, level + 1)
print('\t' * level, node)
display(node.right_child, level + 1)
display(self.root, 0)Would this be correct best practices? The thing that is somewhat confusing to me is switching from using 'self.' for functions relating to the object, to an inner function that doesn't need a 'self.' even though an object is technically calling it. is this already best practice or should I still be using the 'self.'? or is there a better convention to use all together? Thanks.
I have two functions: X and Y.
Y is a somewhat big function that calls X multiple times.
X is only called by Y.
Should I nest the definition of X inside of Y? Or should I leave it out?
Note: I define X as a function because it encapsulates multiple lines of code and I don't want a lot of duplicated code inside Y.
I think that’s mostly a style choice. Personally I would keep it outside; IMO it looks cleaner and more organized. Plus you may be coding down the line and realize “hey, I need that for something else” and if you nest it then you won’t be able to use it.
I typically only use inner functions if I'm trying to do something functional that is easier to read when leveraging the closure. For example, maybe I have a function Y that splits a list into two lists of things bigger and smaller than n, respectively. A helper function X that accumulates a tuple composed of those two lists may be a good angle to take, but it could be seen as redundant to pass the same value for n to recursive calls of X, and X is not particularly useful in any other context since it is so specialized. In that case, especially if the X used multiple times inside of Y, making a lambda a less clear choice, I may decide to nest functions.
Note that this is coming from somebody with more of a functional programming background (OCaml, Idris) when it comes to nested functions, so there may be some more Python-specific considerations as well.
>>> def sum(x, y):
... def do_it():
... return x + y
... return do_it
...
>>> a = sum(1, 3)
>>> a
<function do_it at 0xb772b304>
>>> a()
4
Is this what you were looking for? It's called a closure.
You don't really gain much by doing this, in fact it slows method_a down because it'll define and recompile the other function every time it's called. Given that, it would probably be better to just prefix the function name with underscore to indicate it's a private method -- i.e. _method_b.
I suppose you might want to do this if the nested function's definition varied each time for some reason, but that may indicate a flaw in your design. That said, there is a valid reason to do this to allow the nested function to use arguments that were passed to the outer function but not explicitly passed on to them, which sometimes occurs when writing function decorators, for example. It's what is being shown in the accepted answer although a decorator is not being defined or used.
Update:
Here's proof that nesting them is slower (using Python 3.6.1), although admittedly not by much in this trivial case:
setup = """
class Test(object):
def separate(self, arg):
some_data = self._method_b(arg)
def _method_b(self, arg):
return arg+1
def nested(self, arg):
def method_b2(self, arg):
return arg+1
some_data = method_b2(self, arg)
obj = Test()
"""
from timeit import Timer
print(min(Timer(stmt='obj.separate(42)', setup=setup).repeat())) # -> 0.24479823284461724
print(min(Timer(stmt='obj.nested(42)', setup=setup).repeat())) # -> 0.26553459700452575
Note I added some self arguments to your sample functions to make them more like real methods (although method_b2 still isn't technically a method of the Test class). Also the nested function is actually called in that version, unlike yours.
Been loving Python for 7+ years not and still going strong. I recently found myself writing more and more inner functions to encapsulate logic easier and make otherwise rather polluting/dead functions stick out less.
I like it, because it allows me to write way cleaner and less bloated code – clustering helper-functions to only where they need to be. Also decreases cognitive load considerably by not having to keep track on where a helper function is being used.
But it has gotten to a point where I'm genuinely concerned, because I have also started defining lambdas in inner-functions too! I know lambda shouldn't be used and PEP checker complains too, but it's so handy when combined with list comprehensions…
What are your thoughts on this?
Do you use nested functions yourself or do you consider it bad practice? Where else do you put helpers?
An example would be the following code:
def send_mail(
*,
subject: str,
body_plain: str,
send_to: Union[List[str], str, List[User], User],
send_cc: Optional[Union[List[str], str, List[User], User]] = None,
send_bcc: Optional[Union[List[str], str, List[User], User]] = None,
reply_to: Optional[Union[List[str], str, List[User], User]] = None
...
) -> None:
def _process_recipients(*, recipients: Optional[Union[List[str], str, List[User], User]]) -> List[str]:
""" Process various inputs for `send_to`, `send_cc` and `send_bcc` to a normalized
output that can be used by the emailing instance aka a list of emails """
_user_to_email = lambda x: x.email if isinstance(x, User) else x # transform user objects to mail
if not recipients: return [] # recipients are empty
if isinstance(recipients, str) or isinstance(recipients, User):
recipients = [recipients]
return [_user_to_email(recipient) for recipient in recipients]
...
send_to = _process_recipients(recipients=send_to)
send_cc = _process_recipients(recipients=send_cc)
send_bcc = _process_recipients(recipients=send_bcc)
...
If we have a Solution class, and some def algorithm as the main function the driver function will run, is it a problem if we define a nested say def dfs inside it or should we specifically write a separate __init__ function and a proper DFS function as a method on the class?
In general, when the inner function is small and you want to make it clear that it's only useful to the enclosing function. Alternatively, when you need to return a function. The latter scenario is trivial since generally the inner function relies on variables in the enclosing functions's scope, so declaring it anywhere else isn't an option. You might be able to use a lambda in that case, but anything longer than one expression would need a full function declaration.
Without the implementations it's hard to say which one you should choose in this case. I would hasten to add that you don't have to put everything in a class either.
TL;DR: Use function nesting when you need the characteristics of function nesting
Function Nesting Use Cases (mostly functional idioms, almost certainly incomplete since it's off the top of my head):
- closures
- function factory (programmatic function creation based on parameters)
- creating functions by calling functool.partial
- creating functions by using lambda
- any other reasons you need to create functions during call time
Trade-offs:
- functions are strongly coupled
- the code is always called (unless it's in an if block)
- additional code complexity
- additional runtime cost (potentially, because the inner function get's re-defined with every call to the outer function)
- much harder to extend
- much harder to introspect on the inner function defintion