How To Check If A Key in **kwargs Exists?

后端 未结 6 2034
孤街浪徒
孤街浪徒 2020-12-13 03:09

Python 3.2.3. There were some ideas listed here, which work on regular var\'s, but it seems **kwargs play by different rules... so why doesn\'t this work and how can I check

6条回答
  •  甜味超标
    2020-12-13 04:00

    It is just this:

    if 'errormessage' in kwargs:
        print("yeah it's here")
    

    You need to check, if the key is in the dictionary. The syntax for that is some_key in some_dict (where some_key is something hashable, not necessarily a string).

    The ideas you have linked (these ideas) contained examples for checking if specific key existed in dictionaries returned by locals() and globals(). Your example is similar, because you are checking existence of specific key in kwargs dictionary (the dictionary containing keyword arguments).

提交回复
热议问题