Are the keys of a kwargs argument to Python function guaranteed to be type string?

后端 未结 3 1536
长情又很酷
长情又很酷 2020-12-20 21:01
def func(**kwargs):
   for key, value in kwargs.items():
      # Is key always going to be a string?
      # Could it have spaces?
      pass

Two qu

3条回答
  •  被撕碎了的回忆
    2020-12-20 21:46

    The keywords in kwargs should follow the rules of variable names, full_name is a valid variable name (and a valid keyword), full name is not a valid variable name (and not a valid keyword).

    From PEP 362 -- Function Signature Object:

    A Parameter object has the following public attributes and methods:
    name : str
    - The name of the parameter as a string. Must be a valid python identifier name (with the exception of POSITIONAL_ONLY parameters, which can have it set to None.)

    And, from the Docs:

    2.3. Identifiers and keywords:
    ... Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9. ...

提交回复
热议问题