首页 > 编程语言 > 详细

python判断参数是否是合法标识符

时间:2017-03-20 19:44:42      阅读:234      评论:0      收藏:0      [点我收藏+]

import string


def is_valid_identifier(param):

    alphas = string.letters + ‘_‘

    nums = string.digits


    if len(param) > 1:

        if param[0] not in alphas:

            print ‘invalid:first symbol must be alphabetic‘

        else:

            for otherChar in param[1:]:

                if otherChar not in alphas + nums:

                    print ‘invalid:reminding symbols must be alphanumeric‘

                    break

            else:

                 print ‘okay, %s is an valid identifier‘%param



is_valid_identifier(‘class‘)


备注:除了关键字和一个长度的

python判断参数是否是合法标识符

原文:http://11565901.blog.51cto.com/11555901/1908462

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!