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 web项目意外关闭,但占用端口的问题

问题描述 因为项目强制关闭,但是服务还在运行,导致重新运行项目时候 提示地址已经使用(端口被占用) /usr/bin/python3.5 python-login-demo/inde...

解决Pytorch 训练与测试时爆显存(out of memory)的问题

Pytorch 训练时有时候会因为加载的东西过多而爆显存,有些时候这种情况还可以使用cuda的清理技术进行修整,当然如果模型实在太大,那也没办法。 使用torch.cuda.empty_...

Python的Flask框架及Nginx实现静态文件访问限制功能

Nginx配置 Ngnix,一个高性能的web服务器,毫无疑问它是当下的宠儿。卓越的性能,灵活可扩展,在服务器领域里攻城拔寨,征战天下。 静态文件对于大多数website是不可或缺的一部...

Python 时间处理datetime实例

关于datetime也是简单介绍。因为有很多东西需要自己去使用,去查帮助才最有效。例子:计算上一个星期五并输出。解答:import datetime, calendar ...

python3.3实现乘法表示例

python3.3实现乘法表示例

复制代码 代码如下:from StringHelper  import PadLeft  for x in range(1,10):   ...