python的keyword模块用法实例分析

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

相关文章

在Python程序中操作文件之isatty()方法的使用教程

 如果文件已连接(与终端设备相关联)到一个tty(状)的设备,isatty()方法返回True,否则返回False。 语法 以下是isatty()方法的语法: fileOb...

Pytorch技巧:DataLoader的collate_fn参数使用详解

DataLoader完整的参数表如下: class torch.utils.data.DataLoader( dataset, batch_size=1, shuffle=Fa...

基于Python实现一个简单的银行转账操作

基于Python实现一个简单的银行转账操作

前言 在进行一个应用系统的开发过程中,从上到下一般需要四个构件:客户端-业务逻辑层-数据访问层-数据库,其中数据访问层是一个底层、核心的技术。而且在实际开发中,数据库的操作也就是说数据访...

Django Admin 实现外键过滤的方法

说明和 Model 环境: ➜ python Python 3.6.3 |Anaconda custom (x86_64)| (default, Oct 6 2017...

python WindowsError的错误代码详解

WindowsError的错误代码详解 0操作成功完成。 1功能错误。 2系统找不到指定的文件。 3系统找不到指定的路径。 4系统无法打开文件。 5拒绝访问。 6句柄无效。 7存储控制块...