User Executable Functions in python

I sometimes used (lambda x:<code>)(<some input>)in python to keep a clean namespace (in the global namespace or elsewhere). One problem with the lambda solution is that it is a very limiting construct in terms of what it can contain.

Note. This is a javascript programming habit.

Is this the recommended way to save a namespace? If so, is there a better way to implement the self-fulfillment function?

+7
source share
2 answers

For function A, which will be called only in a specific function B, you can define A in B, by which I think the namespace will not be polluted. eg.

Instead:

def a_fn():
    //do something
def b_fn():
    //do something
def c_fn():
    b_fn()
    a_fn()

You can:

def c_fn():
    def a_fn():
        //do something
    def b_fn():
        //do something
    b_fn()
    a_fn()

, , .

+6

. JavaScript, Python , . , . . , , , .

, . Python, JavaScript, , , , (.. ).

+2

All Articles