0

IIFE Python

Is there an equivalent to an IIFE that I can use in Python? It would be useful for functions that I am calling immediately.

19th Aug 2021, 4:45 PM
NS236
NS236 - avatar
2 Answers
+ 1
Idk whether this was qualified as an IIFE but it seems to work in Code Playground : ) print( ( f := lambda x : x ** 2 )(12) ) print( f( 10 ) )
19th Aug 2021, 10:05 PM
Ipang
+ 1
You can write single-line lambda and execute immediately, but this is not exactly the same and has limitations compared to IIFE which can be a block of code of any size. It would be more pythonic to write an actual real function with def And then you can put a section at the end of your file to actually invoke any functions or statements, by convention this is usually done in an 'if' block like this if __name__ == __main__: execute_anything() You can read more about this here: https://www.freecodecamp.org/news/if-name-main-JUMP_LINK__&&__python__&&__JUMP_LINK-example/
20th Aug 2021, 8:14 PM
Tibor Santa
Tibor Santa - avatar