Variable definition in if

I am trying to call a function in if , store the value in a variable and check if the value is equal to something. Have tried:

 def funk(): return ("Some text") msg="" if (msg=funk()) == "Some resut": print ("Result 1") 

I get a syntax error. It can be done?

I am coming from C and I know this can work:

 #include <stdio.h> char funk() { return 'a'; } int main() { char a; if( a=funk() == 'a' ) printf("Hello, World!\n"); return 0; } 

Is this possible in python?

PS The functions are simple, so it's easier to understand what I want. I will not use some functions in my program. If you give me MINUS, please explain in the comments so I can better ask questions in the future.

+5
source share
1 answer

Python intentionally does not support it because it affects readability. It should be two lines.

 msg = funk() if msg == "Some result": print("Result 1") 
+10
source

Source: https://habr.com/ru/post/1260481/


All Articles