If function A is required only by function B should A be defined inside B?


Simple example. Two methods, one called from another:

def method_a(arg):
    some_data = method_b(arg)

def method_b(arg):
    return some_data

In Python we can declare def inside another def. So, if method_b is required for and called only from method_a, should I declare method_b inside method_a? like this :

def method_a(arg):

    def method_b(arg):
        return some_data

    some_data = method_b

Or should I avoid doing this?

1/14/2017 1:27:21 AM

>>> def sum(x, y):
...     def do_it():
...             return x + y
...     return do_it
>>> a = sum(1, 3)
>>> a
<function do_it at 0xb772b304>
>>> a()

Is this what you were looking for? It's called a closure.

1/28/2011 6:29:58 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow