Python 3:如何检查字符串是否可以是有效变量?

Rad*_*hna 1 python python-3.x

我有一个字符串,想要检查它是否可以用作有效变量而不会出现语法错误.例如

def variableName(string):
    #if string is valid variable name:
        #return True
    #else:
        #return False

input >>> variableName("validVariable")
output >>> True
input >>> variableName("992variable")
output >>> False
Run Code Online (Sandbox Code Playgroud)

我不想使用.isidentifier().我想做一个自己的功能.

DYZ*_*DYZ 6

以下答案仅适用于"旧式"Python-2.7标识符;

"validVariable".isidentifier()
#True
"992variable".isidentifier()
#False
Run Code Online (Sandbox Code Playgroud)

由于您在我发布答案后更改了您的问题,请考虑编写正则表达式:

re.match(r"[_a-z]\w*$", yourstring,flags=re.I)
Run Code Online (Sandbox Code Playgroud)


Ash*_*ary 5

在 Python 3 中,有效标识符可以包含 ASCII 范围之外的字符,因为您不想使用str.isidentifier,您可以在 Python 中编写自己的版本。

\n

其规范可以在这里找到: https: //www.python.org/dev/peps/pep-3131/#specification-of-language-changes

\n

执行:

\n
import keyword\nimport re\nimport unicodedata\n\n\ndef is_other_id_start(char):\n    """\n    Item belongs to Other_ID_Start in\n    http://unicode.org/Public/UNIDATA/PropList.txt\n    """\n    return bool(re.match(r\'[\\u1885-\\u1886\\u2118\\u212E\\u309B-\\u309C]\', char))\n\n\ndef is_other_id_continue(char):\n    """\n    Item belongs to Other_ID_Continue in\n    http://unicode.org/Public/UNIDATA/PropList.txt\n    """\n    return bool(re.match(r\'[\\u00B7\\u0387\\u1369-\\u1371\\u19DA]\', char))\n\n\ndef is_xid_start(char):\n\n    # ID_Start is defined as all characters having one of\n    # the general categories uppercase letters(Lu), lowercase\n    # letters(Ll), titlecase letters(Lt), modifier letters(Lm),\n    # other letters(Lo), letter numbers(Nl), the underscore, and\n    # characters carrying the Other_ID_Start property. XID_Start\n    # then closes this set under normalization, by removing all\n    # characters whose NFKC normalization is not of the form\n    # ID_Start ID_Continue * anymore.\n\n    category = unicodedata.category(char)\n    return (\n        category in {\'Lu\', \'Ll\', \'Lt\', \'Lm\', \'Lo\', \'Nl\'} or\n        is_other_id_start(char)\n    )\n\n\ndef is_xid_continue(char):\n    # ID_Continue is defined as all characters in ID_Start, plus\n    # nonspacing marks (Mn), spacing combining marks (Mc), decimal\n    # number (Nd), connector punctuations (Pc), and characters\n    # carryig the Other_ID_Continue property. Again, XID_Continue\n    # closes this set under NFKC-normalization; it also adds U+00B7\n    # to support Catalan.\n\n    category = unicodedata.category(char)\n    return (\n        is_xid_start(char) or\n        category in {\'Mn\', \'Mc\', \'Nd\', \'Pc\'} or\n        is_other_id_continue(char)\n    )\n\n\ndef is_valid_identifier(name):\n    # All identifiers are converted into the normal form NFKC\n    # while parsing; comparison of identifiers is based on NFKC.\n    name = unicodedata.normalize(\n        \'NFKC\', name\n    )\n\n    # check if it\'s a keyword\n    if keyword.iskeyword(name):\n        return False\n\n    # The identifier syntax is <XID_Start> <XID_Continue>*.\n    if not (is_xid_start(name[0]) or name[0] == \'_\'):\n        return False\n\n    return all(is_xid_continue(char) for char in name[1:])\n\nif __name__ == \'__main__\':\n    # From goo.gl/pvpYg6\n    assert is_valid_identifier("a") is True\n    assert is_valid_identifier("Z") is True\n    assert is_valid_identifier("_") is True\n    assert is_valid_identifier("b0") is True\n    assert is_valid_identifier("bc") is True\n    assert is_valid_identifier("b_") is True\n    assert is_valid_identifier("\xc2\xb5") is True\n    assert is_valid_identifier("") is True\n\n    assert is_valid_identifier(" ") is False\n    assert is_valid_identifier("[") is False\n    assert is_valid_identifier("\xc2\xa9") is False\n    assert is_valid_identifier("0") is False\n
Run Code Online (Sandbox Code Playgroud)\n
\n

您可以分别在此处此处查看 CPython 和 Pypy 的实现。

\n