Retrieving Python Keywords

Discussion in 'Python' started by candide, Apr 10, 2011.

  1. candide

    candide Guest

    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, ...).
     
    candide, Apr 10, 2011
    #1
    1. Advertising

  2. candide

    Terry Reedy Guest

    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
     
    Terry Reedy, Apr 10, 2011
    #2
    1. Advertising

  3. 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
     
    Steven D'Aprano, Apr 10, 2011
    #3
  4. candide

    John Connor Guest

    Actually this is all it takes:
    import keywords
    print keywords.kwlist

    --jac

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

    >
    > 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
    >
     
    John Connor, Apr 10, 2011
    #4
  5. candide

    candide Guest

    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.
     
    candide, Apr 10, 2011
    #5
  6. candide

    candide Guest

    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.
     
    candide, Apr 10, 2011
    #6
  7. candide

    Terry Reedy Guest

    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
     
    Terry Reedy, Apr 10, 2011
    #7
  8. On Sun, 10 Apr 2011 11:12:46 +0200, candide <>
    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://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Apr 10, 2011
    #8
  9. candide

    Glazner Guest

    On Apr 10, 4:28 am, 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
    >>> 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']
     
    Glazner, Apr 11, 2011
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andr? Roberge
    Replies:
    8
    Views:
    342
    Sridhar R
    Oct 9, 2004
  2. Roy Smith

    Python keywords vs. English grammar

    Roy Smith, May 24, 2006, in forum: Python
    Replies:
    7
    Views:
    352
    Roy Smith
    May 25, 2006
  3. gtb

    Python keywords

    gtb, Apr 26, 2007, in forum: Python
    Replies:
    6
    Views:
    508
    Alex Martelli
    Apr 27, 2007
  4. Replies:
    6
    Views:
    335
    Stargaming
    Jun 22, 2007
  5. Replies:
    6
    Views:
    395
    Michael Hoffman
    Jul 2, 2007
Loading...

Share This Page