Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Retrieving Python Keywords

Reply
Thread Tools

Retrieving Python Keywords

 
 
candide
Guest
Posts: n/a
 
      04-10-2011
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, ...).
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      04-10-2011
On 4/9/2011 9:28 PM, candide wrote:
> 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

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-10-2011
On Sun, 10 Apr 2011 03:28:10 +0200, candide wrote:

> 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


--
Steven
 
Reply With Quote
 
John Connor
Guest
Posts: n/a
 
      04-10-2011
Actually this is all it takes:
import keywords
print keywords.kwlist

--jac

On Sat, Apr 9, 2011 at 8:57 PM, Chris Angelico <(E-Mail Removed)> wrote:
> On Sun, Apr 10, 2011 at 11:28 AM, candide <(E-Mail Removed)> wrote:
>> 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, ...).

>
> I don't know about any other way, but here's a really REALLY stupid
> method. For every possible alphabetic string, attempt to eval() it; if
> you get NameError or no error at all, then it's not a keyword.
> SyntaxError means it's a keyword.
>
>>>> eval("foo")

>
> Traceback (most recent call last):
> *File "<pyshell#3>", line 1, in <module>
> * *eval("foo")
> *File "<string>", line 1, in <module>
> NameError: name 'foo' is not defined
>>>> eval("lambda")

>
> Traceback (most recent call last):
> *File "<pyshell#7>", line 1, in <module>
> * *eval("lambda")
> *File "<string>", line 1
> * *lambda
> * * * * ^
> SyntaxError: unexpected EOF while parsing
>>>> eval("eval")

> <built-in function eval>
>
> Yes, it's stupid. But I'm feeling rather mischievous today.
>
> Chris Angelico
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
candide
Guest
Posts: n/a
 
      04-10-2011
Le 10/04/2011 04:09, John Connor a écrit :
> Actually this is all it takes:
> import keywords
> print keywords.kwlist



>>> import keywords

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named keywords
>>>



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.
 
Reply With Quote
 
candide
Guest
Posts: n/a
 
      04-10-2011
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.

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      04-10-2011
On 4/10/2011 5:12 AM, candide wrote:
> 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/


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)'.

>> or try the obvious imports


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.

--
Terry Jan Reedy


 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      04-10-2011
On Sun, 10 Apr 2011 11:12:46 +0200, candide <(E-Mail Removed)>
declaimed the following in gmane.comp.python.general:

> 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
>


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
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Glazner
Guest
Posts: n/a
 
      04-11-2011
On Apr 10, 4:28*am, candide <(E-Mail Removed)> wrote:
> 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
>>> 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']
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python changing keywords name vedrandekovic@v-programs.com Python 6 07-02-2007 01:26 PM
Changing the names of python keywords and functions vedrandekovic@v-programs.com Python 6 06-22-2007 03:39 PM
Python keywords gtb Python 6 04-27-2007 05:15 AM
Python keywords vs. English grammar Roy Smith Python 7 05-25-2006 12:05 AM
Desactivating Python keywords and built-in functions Andr? Roberge Python 8 10-09-2004 01:44 AM



Advertisments