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

相关文章

对django2.0 关联表的必填on_delete参数的含义解析

一对多(ForeignKey) class ForeignKey(ForeignObject): def __init__(self, to, on_delete, relate...

Python 使用Numpy对矩阵进行转置的方法

Python 使用Numpy对矩阵进行转置的方法

如下所示: matrix.py #!/usr/bin/python # -*- encoding:UTF-8-*- import pprint import numpy as np...

Python实现最大子序和的方法示例

Python实现最大子序和的方法示例

描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大。 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,...

不要用强制方法杀掉python线程

前言:     不要试图用强制方法杀掉一个python线程,这从服务设计上就存在不合理性。 多线程本用来任务的协作并发,如果你使用强制手段干掉线程,那么很大...

python类继承用法实例分析

本文实例讲述了python类继承用法。分享给大家供大家参考。具体如下: help('object') # test class Class1(object): """ Cla...