python的keyword模块用法实例分析

yipeiwu_com5年前Python基础

本文实例讲述了python的keyword模块用法。分享给大家供大家参考。具体如下:

Help on module keyword:
NAME
  keyword - Keywords (from "graminit.c")
FILE
  /usr/lib64/python2.6/keyword.py
DESCRIPTION
  This file is automatically generated; please don't muck it up!
  To update the symbols in this file, 'cd' to the top directory of
  the python source tree after building the interpreter and run:
    python Lib/keyword.py
FUNCTIONS
  iskeyword = __contains__(...)
    x.__contains__(y) <==> y in x.
DATA
  __all__ = ['iskeyword', 'kwlist']
  kwlist = ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', ...

得到python的关键字列表:

>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

判断字符串是否是python的关键字

>>> keyword.iskeyword('and')
True
>>> 
>>> keyword.iskeyword('has')
False

希望本文所述对大家的Python程序设计有所帮助。

相关文章

django创建简单的页面响应实例教程

django创建简单的页面响应实例教程

首先 编辑views.py文件 每个响应对应一个函数 函数必须返回一个响应 函数必须存在一个参数 一般约定为request 每个响应函数 对应一个URL from django...

对Python中plt的画图函数详解

1、plt.legend plt.legend(loc=0)#显示图例的位置,自适应方式 说明: 'best' : 0, (only implemented for ax...

python自动化之Ansible的安装教程

本文实例讲述了python自动化之Ansible的安装。分享给大家供大家参考,具体如下: 一 点睛 Ansible只需在管理端部署环境即可,建议采用yum源方式来实现部署。 二 安装An...

python写入文件自动换行问题的方法

python写入文件自动换行问题的方法

现在需要一个写文件方法,将selenium的脚本运行结果写入test_result.log文件中 首先创建写入方法 def write_result(str): writeres...

Ubuntu+python将nii图像保存成png格式

这里介绍一个nii文件保存为png格式的方法。 这篇文章是介绍多个nii文件保存为png格式的方法: /post/165692.htm 系统:Ubuntu 16.04 软件: pytho...