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程序设计有所帮助。

相关文章

Mac中Python 3环境下安装scrapy的方法教程

Mac中Python 3环境下安装scrapy的方法教程

前言 最近抽空想学习一下python的爬虫框架scrapy,在mac下安装的时候遇到了问题,逐一解决了问题,分享一下,话不多说了,来一起看看详细的介绍吧。 步骤如下: 1. 从官网 下载...

Python中time模块与datetime模块在使用中的不同之处

Python 中提供了对时间日期的多种多样的处理方式,主要是在 time 和 datetime 这两个模块里。今天稍微梳理一下这两个模块在使用上的一些区别和联系。 time 在 Pyth...

Python学习笔记之字符串和字符串方法实例详解

Python学习笔记之字符串和字符串方法实例详解

本文实例讲述了Python学习笔记之字符串和字符串方法。分享给大家供大家参考,具体如下: 字符串 在 python 中,字符串的变量类型显示为 str。你可以使用双引号 " 或单引号 '...

Python面向对象之类的封装操作示例

本文实例讲述了Python面向对象之类的封装操作。分享给大家供大家参考,具体如下: 承接上一节《Python面向对象之类和实例》,学了Student类的定义及实例化,每个实例都拥有各自的...

简单了解python的内存管理机制

Python引入了一个机制:引用计数。 引用计数 python内部使用引用计数,来保持追踪内存中的对象,Python内部记录了对象有多少个引用,即引用计数,当对象被创建时就创建了一个...