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

相关文章

解决已经安装requests,却依然提示No module named requests问题

Python版本3.5.1, pip install requests 之后依然提示 Python ImportError: No module named 'requests' 经过文...

对PyTorch torch.stack的实例讲解

不是concat的意思 import torch a = torch.ones([1,2]) b = torch.ones([1,2]) torch.stack([a,b],1) (...

Python实现短网址ShortUrl的Hash运算实例讲解

本文实例讲述了Python实现短网址ShortUrl的Hash运算方法。分享给大家供大家参考。具体如下: shorturl实现常见的做法都是将原始Url存储到数据库,由数据库返回一个对应...

Python模仿POST提交HTTP数据及使用Cookie值的方法

本文实例讲述了在Python中模仿POST HTTP数据及带Cookie提交数据的实现方法,分享给大家供大家参考。具体实现方法如下: 方法一 如果不使用Cookie, 发送HTTP PO...

Python实现嵌套列表及字典并按某一元素去重复功能示例

Python实现嵌套列表及字典并按某一元素去重复功能示例

本文实例讲述了Python实现嵌套列表及字典并按某一元素去重复功能。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 cla...