检查传递给函数的关键字参数的真实性并不像预期的那样有效为什么?

2024-09-27 21:33:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道how to check如果我输入**kwargs存在。现在我要检查传递给函数的参数的值。你知道吗

def examplefunc(x,y,**kwargs):

    print(kwargs['extraarg'])
    if 'extraarg' in kwargs == True:
        print(kwargs['extraarg'])
        print("This is not printed")

    if 'extraarg' in kwargs: print("This is printed")

examplefunc(3,2,extraarg=True)   

输出:

True

This is printed

为什么This is not printed不打印?'extraarg' in kwargs为false。那么为什么要继续打印This is printed?你知道吗


我还尝试用if 'extraarg' == True:替换if 'extraarg' in kwargs == True行,但是输出仍然缺少This is not printed。你知道吗


Tags: to函数intrueifischecknot
1条回答
网友
1楼 · 发布于 2024-09-27 21:33:30
def examplefunc(x,y,**kwargs):

    print(kwargs['extraarg'])
    if ('extraarg' in kwargs)==True
        print(kwargs['extraarg'])
        print("This is not printed")

    if 'extraarg' in kwargs: print("This is printed")



if __name__ == "__main__":

    examplefunc(3,2,extraarg=True) 

输出:

True
True
This is not printed
This is printed

相关问题 更多 >

    热门问题