Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why does "help(import)" not work?

6 views
Skip to first unread message

Robert P. J. Day

unread,
Nov 6, 2009, 4:56:34 AM11/6/09
to Python Mailing List

i'm sure there's a painfully obvious answer to this, but is there a
reason i can't do:

>>> help(import)
File "<stdin>", line 1
help(import)
^
SyntaxError: invalid syntax
>>>

on the other hand, i can certainly go into "help()" and type
"import" to get that help. it seems counter-intuitive to have the
first variation fail but the second succeed.

what is the rule for this in python3?

rday
--


========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================

Simon Brunning

unread,
Nov 6, 2009, 5:10:37 AM11/6/09
to Python Mailing List
2009/11/6 Robert P. J. Day <rpj...@crashcourse.ca>:

>
>  i'm sure there's a painfully obvious answer to this, but is there a
> reason i can't do:
>
>>>> help(import)
>  File "<stdin>", line 1
>    help(import)
>              ^
> SyntaxError: invalid syntax

import is a keyword, not an object.

--
Cheers,
Simon B.

Diez B. Roggisch

unread,
Nov 6, 2009, 5:11:44 AM11/6/09
to
Robert P. J. Day schrieb:

> i'm sure there's a painfully obvious answer to this, but is there a
> reason i can't do:
>
>>>> help(import)
> File "<stdin>", line 1
> help(import)
> ^
> SyntaxError: invalid syntax
>
> on the other hand, i can certainly go into "help()" and type
> "import" to get that help. it seems counter-intuitive to have the
> first variation fail but the second succeed.
>
> what is the rule for this in python3?

import is a keyword, so the parser can't comprehend the expression you
created. In the same way it can't do it for


help(class)

or

help(def)


or any other keyword.

Diez

Chris Rebert

unread,
Nov 6, 2009, 5:12:09 AM11/6/09
to Robert P. J. Day, Python Mailing List
On Fri, Nov 6, 2009 at 1:56 AM, Robert P. J. Day <rpj...@crashcourse.ca> wrote:
>  i'm sure there's a painfully obvious answer to this, but is there a
> reason i can't do:
>
>>>> help(import)
>  File "<stdin>", line 1
>    help(import)
>              ^
> SyntaxError: invalid syntax
>>>>
>
>  on the other hand, i can certainly go into "help()" and type
> "import" to get that help.  it seems counter-intuitive to have the
> first variation fail but the second succeed.
>
>  what is the rule for this in python3?

"Special cases aren't special enough to break the rules."
-- Zen of Python (http://www.python.org/dev/peps/pep-0020/)

It would take a hideous kludge to make help(import) work.

Cheers,
Chris
--
http://blog.rebertia.com

Bruno Desthuilliers

unread,
Nov 6, 2009, 5:12:38 AM11/6/09
to
Robert P. J. Day a �crit :

> i'm sure there's a painfully obvious answer to this, but is there a
> reason i can't do:
>
>>>> help(import)

Yes, there's a very painfully obvious reason : 'import' is a statement,
'help' is a function, and you can't (syntactically) pass a statement as
an argument to function !-)

> File "<stdin>", line 1
> help(import)
> ^
> SyntaxError: invalid syntax
>
> on the other hand, i can certainly go into "help()" and type
> "import" to get that help. it seems counter-intuitive to have the
> first variation fail but the second succeed.

Would you find it more "intuitive" to have different syntactic rules for
the interactive shell ?-)

FWIW, the way to get help on a statement (or on a non-already defined
name) is to pass a string to help, ie:

help("import")

HTH

Brian Quinlan

unread,
Nov 6, 2009, 5:14:02 AM11/6/09
to Robert P. J. Day, Python Mailing List
Hi Robert,

help() is just a regular function that must be called with correct
Python syntax and the import keyword is not allowed in an argument list.

The correct syntax is:
help('import')

Cheers,
Brian

On 6 Nov 2009, at 20:56, Robert P. J. Day wrote:

>
> i'm sure there's a painfully obvious answer to this, but is there a
> reason i can't do:
>
>>>> help(import)

> File "<stdin>", line 1
> help(import)
> ^
> SyntaxError: invalid syntax
>>>>
>
> on the other hand, i can certainly go into "help()" and type
> "import" to get that help. it seems counter-intuitive to have the
> first variation fail but the second succeed.
>

> what is the rule for this in python3?
>

> rday
> --
>
>
> =
> =

> ======================================================================
> Robert P. J. Day Waterloo, Ontario,
> CANADA
>
> Linux Consulting, Training and Kernel Pedantry.
>
> Web page: http://crashcourse.ca
> Twitter: http://twitter.com/rpjday
> =
> =
> ======================================================================

> --
> http://mail.python.org/mailman/listinfo/python-list

Robert P. J. Day

unread,
Nov 6, 2009, 5:28:19 AM11/6/09
to Chris Rebert, Python Mailing List
On Fri, 6 Nov 2009, Chris Rebert wrote:

> On Fri, Nov 6, 2009 at 1:56 AM, Robert P. J. Day <rpj...@crashcourse.ca> wrote:
> >  i'm sure there's a painfully obvious answer to this, but is there a
> > reason i can't do:
> >
> >>>> help(import)
> >  File "<stdin>", line 1
> >    help(import)
> >              ^
> > SyntaxError: invalid syntax
> >>>>

> It would take a hideous kludge to make help(import) work.
>
> Cheers,
> Chris

um ... ok, i'll take your word for it, but is there at least a
generalizable pattern for what is directly help()able and what isn't?

rday
--

========================================================================

Robert P. J. Day

unread,
Nov 6, 2009, 5:31:50 AM11/6/09
to Brian Quinlan, Python Mailing List
On Fri, 6 Nov 2009, Brian Quinlan wrote:

> Hi Robert,
>
> help() is just a regular function that must be called with correct
> Python syntax and the import keyword is not allowed in an argument
> list.
>
> The correct syntax is:
> help('import')

ah, ok, now it makes sense. so python3 just lets me be sloppy about
it, as in:

>>> help(print)

got it.

Chris Rebert

unread,
Nov 6, 2009, 5:36:14 AM11/6/09
to Robert P. J. Day, Python Mailing List
On Fri, Nov 6, 2009 at 2:28 AM, Robert P. J. Day <rpj...@crashcourse.ca> wrote:
> On Fri, 6 Nov 2009, Chris Rebert wrote:
>
>> On Fri, Nov 6, 2009 at 1:56 AM, Robert P. J. Day <rpj...@crashcourse.ca> wrote:
>> >  i'm sure there's a painfully obvious answer to this, but is there a
>> > reason i can't do:
>> >
>> >>>> help(import)
>> >  File "<stdin>", line 1
>> >    help(import)
>> >              ^
>> > SyntaxError: invalid syntax
>> >>>>
>
>> It would take a hideous kludge to make help(import) work.
>
>  um ... ok, i'll take your word for it, but is there at least a
> generalizable pattern for what is directly help()able and what isn't?

Language keywords aren't help()-able:
http://docs.python.org/reference/lexical_analysis.html#keywords

Neither are punctuation/operators (see other sections on same webpage).

help() is run-of-the-mill function, no different than any other, is
not treated specially, and doesn't perform magic.
You can't pass raw syntax elements to functions (whether the functions
are built-in or user-defined).

Robert P. J. Day

unread,
Nov 6, 2009, 5:36:21 AM11/6/09
to Chris Rebert, Python Mailing List
On Fri, 6 Nov 2009, Robert P. J. Day wrote:

> On Fri, 6 Nov 2009, Chris Rebert wrote:
>
> > On Fri, Nov 6, 2009 at 1:56 AM, Robert P. J. Day <rpj...@crashcourse.ca> wrote:
> > >  i'm sure there's a painfully obvious answer to this, but is there a
> > > reason i can't do:
> > >
> > >>>> help(import)
> > >  File "<stdin>", line 1
> > >    help(import)
> > >              ^
> > > SyntaxError: invalid syntax
> > >>>>
>
> > It would take a hideous kludge to make help(import) work.
> >

> > Cheers,
> > Chris


>
> um ... ok, i'll take your word for it, but is there at least a
> generalizable pattern for what is directly help()able and what
> isn't?

never mind, i was just educated on the proper usage of help(). i
*knew* i was going to embarrass myself when i asked that. :-P back
to reading.

Simon Brunning

unread,
Nov 6, 2009, 5:45:38 AM11/6/09
to Python Mailing List
2009/11/6 Robert P. J. Day <rpj...@crashcourse.ca>:

>  um ... ok, i'll take your word for it, but is there at least a
> generalizable pattern for what is directly help()able and what isn't?

If it's an object (or to be precise, a name bound to an object), it's
helpable. If it's a keyword, it's not - and in fact can't be, since as
you've found, it's a syntax error - so you fall back to passing a
string.

--
Cheers,
Simon B.

Simon Brunning

unread,
Nov 6, 2009, 5:47:00 AM11/6/09
to Python Mailing List
2009/11/6 Robert P. J. Day <rpj...@crashcourse.ca>:
>  ah, ok, now it makes sense.  so python3 just lets me be sloppy about
> it, as in:

print is a function in Python 3, so that's fine. In Python 2, that
would also be a syntax error.

--
Cheers,
Simon B.

Paul Rudin

unread,
Nov 6, 2009, 5:49:17 AM11/6/09
to
"Robert P. J. Day" <rpj...@crashcourse.ca> writes:

> On Fri, 6 Nov 2009, Brian Quinlan wrote:
>
>> Hi Robert,
>>
>> help() is just a regular function that must be called with correct
>> Python syntax and the import keyword is not allowed in an argument
>> list.
>>
>> The correct syntax is:
>> help('import')
>
> ah, ok, now it makes sense. so python3 just lets me be sloppy about
> it, as in:
>
> >>> help(print)
>
> got it.

In python 3 print is a function.

Sion Arrowsmith

unread,
Nov 6, 2009, 5:51:46 AM11/6/09
to
Robert P. J. Day <rpj...@crashcourse.ca> wrote:
>On Fri, 6 Nov 2009, Brian Quinlan wrote:
>> The correct syntax is:
>> help('import')
>
> ah, ok, now it makes sense. so python3 just lets me be sloppy about
>it, as in:
>
> >>> help(print)

No, "print" is a function in python3, not a statement as it is in
earlier versions. There's nothing sloppy about it, and nothing in
help has changed from 2 to 3: if it's a language keyword, you need
to pass help it's name; if it's a function or a class, you can pass
help the object itself.

--
\S

under construction

Bruno Desthuilliers

unread,
Nov 6, 2009, 10:15:11 AM11/6/09
to
Chris Rebert a écrit :

> On Fri, Nov 6, 2009 at 2:28 AM, Robert P. J. Day <rpj...@crashcourse.ca> wrote:
>
>> um ... ok, i'll take your word for it, but is there at least a
>> generalizable pattern for what is directly help()able and what isn't?
>
> Language keywords aren't help()-able:
> http://docs.python.org/reference/lexical_analysis.html#keywords
> Neither are punctuation/operators (see other sections on same webpage).

Ever tried help("import") or help("if") or help("+") ?

Chris Rebert

unread,
Nov 6, 2009, 1:04:21 PM11/6/09
to pytho...@python.org

help()-able in this context meaning you can drop the quotes and not
get a syntax error.

0 new messages