def valid_identifier(name):
"""Determines whether the given name could be used a Python identifier"""
try:
name = ensure_unicode(name)
except ValueError:
return False
if name in __KEYWORDS:
return False
try:
root = _ast.parse(name)
except:
return False
return (isinstance(root, _ast.Module) and
len(root.body) == 1 and
isinstance(root.body[0], _ast.Expr) and
isinstance(root.body[0].value, _ast.Name) and
root.body[0].value.id == name)
评论列表
文章目录