17

I have used occasionally (lambda x:<code>)(<some input>) in python, to preserve my namespace's (within the global namespace or elsewhere) cleanliness. One issue with the lambda solution is that it is a very limiting construct in terms of what it may contain.

Note: This is a habit from javascript programming

Is this a recommended way of preserving namespace? If so, is there a better way to implement a self-executing function?

2
  • In what situation do you want to do this where you couldn't just write a separate function and call it? Commented Aug 21, 2012 at 6:24
  • For example, if I wanted to string several function calls inside each other, but didn't want to pollute the namespace with a function that would only be called in a specific place. Commented Aug 21, 2012 at 6:26

3 Answers 3

13

Regarding the second half of the question

is there a better way to implement a self-executing function?

The standard way (<function-expression>)() is not possible in Python, because there is no way to put a multi-line block into a bracket without breaking Python's fundamental syntax. Nonetheless, Python do recognize the need for using function definitions as expressions and provide decorators (PEP318) as an alternative. PEP318 has an extensive discussion on this issue, in case someone would like to read more.

With decorators, it would be like

evalfn = lambda f: f()

@evalfn
def _():
    print('I execute immediately')

Although vastly different syntatically, we shall see that it really is the same: the function definition is anonimous and used as an expression.

Using decorator for self-excuting functions is a bit of overkill, compared to the let-call-del method shown below. However, it may worth a try if there are many self-execution functions, a self-executing function is getting too long, or you simply don't bother naming these self-executing functions.

def f():
    print('I execute immediately')
f()
del f
Sign up to request clarification or add additional context in comments.

Comments

5

For a function A that 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. e.g.,

Instead of :

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()

Though I'm not sure if its the pythonic way, I usually do like this.

5 Comments

I never realized this, coming from a javascript background, but I just realized that python creates namespaces for functions.
@Justanotherdunce: Functions defined inside a function are simply additional local variables. In effect, this is a little like a namespace.
However, this is no different from what JavaScript does.
Isn't there anything like main function like in Java in python that allows self execution?
this is not solution, because, we must call c_fn()
1

You don't do it. It's a good in JavaScript, but in Python, you haven either lightweight syntax nor a need for it. If you need a function scope, define a function and call it. But very often you don't need one. You may need to pull code apart into multiple function to make it more understandable, but then a name for it helps anyway, and it may be useful in more than one place.

Also, don't worry about adding some more names to a namespace. Python, unlike JavaScript, has proper namespaces, so a helper you define at module scope is not visible in other files by default (i.e. unless imported).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.