2 Réponses
+ 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 ) )
+ 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/