Retrieving Python Keywords

C

candide

Python is very good at introspection, so I was wondering if Python (2.7)
provides any feature to retrieve the list of its keywords (and, as,
assert, break, ...).
 
T

Terry Reedy

Python is very good at introspection, so I was wondering if Python (2.7)
provides any feature to retrieve the list of its keywords (and, as,
assert, break, ...).

Yes. (Look in the manuals, or try the obvious imports ;-)
 
S

Steven D'Aprano

Python is very good at introspection, so I was wondering if Python (2.7)
provides any feature to retrieve the list of its keywords (and, as,
assert, break, ...).

import keyword
 
C

candide

Le 10/04/2011 04:09, John Connor a écrit :
Actually this is all it takes:
import keywords
print keywords.kwlist
Traceback (most recent call last):


so I considered first it was a joke ! ;) In fact the import doesn't
need plural, and... Python is very very introspective ;)


Thanks and thanks to Steven too.
 
C

candide

Le 10/04/2011 04:01, Terry Reedy a écrit :
Yes. (Look in the manuals,


I did : my main reference book is the Martelli's /Python in a Nutshell/
and the index doesn't refer to the keyword import


or try the obvious imports ;-)

The only obvious I saw was sys module.
 
T

Terry Reedy

Le 10/04/2011 04:01, Terry Reedy a écrit :
I did : my main reference book is the Martelli's /Python in a Nutshell/

You should only use that as a supplement.
and the index doesn't refer to the keyword import

and now you know why ;-).

I meant the fine, heavily edited and constantly improved by 20+ people
manuals that come with Python. The Global Module Index has one entry
under K -- the keyword module. The General Index has multiple entries
for 'keyword', including 'keyword(module)'.

I meant, 'import keyword' or 'import keywords'.
Sorry, I guess perhaps not so obvious if one is not used to Python's
extreme introspection features.
 
D

Dennis Lee Bieber

Le 10/04/2011 04:01, Terry Reedy a écrit :



I did : my main reference book is the Martelli's /Python in a Nutshell/
and the index doesn't refer to the keyword import

The MAIN reference for language specific details should be the
language reference manual that should have come with the installation.
Nutshell mostly expands on the library features.

Section 2.3.1 of the language reference manual is titled: Keywords
 
G

Glazner

Python is very good at introspection, so I was wondering if Python (2.7)
provides any feature to retrieve the list of its keywords (and, as,
assert, break, ...).
['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']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top