Python标识符必须以字母或下划线开头,后面跟字母、下划线或者数字,且标识符不能为关键字,如何有效的检查?
#!/usr/bin/env python
'''
$Id$
idcheck.py -- checks identifiers for validity
This application is limited in that it currently only supports
checking identifiers with length > 1 (does not process identifiers
of length greater than 1. This application also does not recognize
do keywords.
Exercise:
6-2) update this script to process identifiers of length 1
as well as recognizing keywords as invalid identifiers
(for use by the programmer; they are valid Python
identifiers otherwise).
'''
import string # string utility module
# create alphabet and number sets
alphas = string.letters + '_'
nums = string.digits
# salutation message and input prompt
print 'Welcome to the Identifier Checker v1.0'
print 'Testees must be at least 2 chars long.'
inp = raw_input('Identifier to test >>> ')
# only take action for identifiers with length >= 1
if len(inp) >= 1:
# first character must be alphabetic
if inp[0] not in alphas:
print 'invalid: first symbol must be alphabetic'
# remaning characters can be alphanumeric
else:
for otherChar in inp[1:]:
if otherChar not in alphas + nums:
print 'invalid: remaining symbols must be alphanumeric'
break
else:
print "okay as an identifier"
else:
print 'invalid: length must be >= 1'
(2)运行结果
其中for-else结构中else语句是一个可选项,它只在 for 循环完整的结束而没有遇到break 时执行。
性能:for otherChar in inp[1:]:循环中的 if 语句执行了合并两个字符串的操作,被合并的这两个字符串从始至终就没变过,而每次都会重新进行一次计算,需要创建新对象,如果先把这两个字符串存为一个新字符串,就可以直接引用这个字符串而不用进行重复计算了。所以可以改为:
alphnums = alphas + nums
for otherChar in inp[1:]:
if otherChar not in alphnums:
print 'invalid: remaining symbols must be alphanumeric'
break
else:
print "okay as an identifie
#!/usr/bin/env python
from keyword import kwlist
import string
ALPHAS = string.ascii_letters + '_'
NUMS = string.digits
def main():
print 'Welcome to the Identifier Checker v2.0'
myInput = raw_input('Identifier to test >>>').strip()
if len(myInput) == 0:
print "ERROR: no identifier candidate entered"
return
if myInput in kwlist:
print "ERROR: %r is a keyword" % myInput
return
alnums = ALPHAS + NUMS
for i, c in enumerate(myInput):
if i == 0 and c not in ALPHAS:
print 'ERROR: first symbol must be alphabetic'
break
if c not in alnums:
print 'ERROR: remaining symbols must be alphanumeric'
break
else:
print "okay as an identifier"
if __name__ == '__main__':
main()
其中Python的关键字列表为:
(2)运行结果