在Python中定义嵌套函数

我正在通过介绍嵌套函数的DataCamp课程进行学习。讨论转向在另一个函数内部创建一个函数的想法,如下所示:

def foo(thing):
    def bar(other_thing):
        third_thing = other_thing*2    
        return third_thing
    return bar

While I understand the idea of a nested function, isn't it unnecessarily complex to add a function definition inside a function? Shouldn't the function bar() be defined outside function foo() and only called inside the function, thus avoiding the wasted time of defining the function bar() with each function call of foo()? Or does defining the function bar() inside function foo() save the memory that would be taken up by defining bar() globally?

最佳做法是什么? ...还是我基本上只是概述了两个问题,最终的方法取决于程序员的判断力?