0

A great feature of Javascript is function statements. You can do this:

(function myFunc(){
 doSomething();
 doSomethingElse();
})(); 

Which is a way to declare a function and call it without having to refer to it twice. Can that be mimified on Python?

asked Mar 31, 2012 at 5:21
1
  • You've got it the wrong way around. The function in your snippet is used as expression (which contains statements), and function definition being an expression is precisely what one would need to do the same thing in Python. def is a statement in Python. Commented Mar 31, 2012 at 8:56

1 Answer 1

2

Python does not have this syntax for functions. It does, however, support the lambda syntax which can be used in this way. However, a lambda is an expression and not a statement, so it is limited.

(lambda x: x*x)(10)

would return 100

When this feature is needed, just use the def statement to define a function

...
def tmp(x,y):
 foo(x);
 foo(y);
tmp(12,24)
...
answered Mar 31, 2012 at 5:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.