在if中定义变量

kem*_*mis 5 python conditional if-statement variable-assignment

我试图在一个函数中调用一个函数if,将值保存在变量中并检查该值是否等于某个值.试过:

def funk():
    return ("Some text")

msg=""
if (msg=funk()) == "Some resut":
  print ("Result 1")
Run Code Online (Sandbox Code Playgroud)

我收到语法错误.可以这样做吗?

我来自C,我知道这可行:

#include <stdio.h>

char funk()
{
    return 'a';
}
int main()
{
    char a;
    if( a=funk() == 'a' )
        printf("Hello, World!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这在python中可能吗?

PS功能很简单,因此更容易理解我想要的东西.我不会在程序中使用某些功能.如果你给我一个MINUS请在评论中解释,以便我将来可以提出更好的问题

Joh*_*ica 10

Python故意不支持它,因为它会损害可读性.它需要两行.

msg = funk()
if msg == "Some result":
    print("Result 1")
Run Code Online (Sandbox Code Playgroud)