检查传递给函数的关键字参数的真实性不符合预期-为什么?

时间:2018-09-25 11:29:58

标签: python python-3.x function kwargs

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

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)   

输出:

  

     

已打印

为什么不打印This is not printed'extraarg' in kwargs是错误的。那么为什么要继续打印This is printed


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

1 个答案:

答案 0 :(得分:-1)

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