Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77307

Re: Typing help brings up a screen that says type help()

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Typing help brings up a screen that says type help()
Date 2014-08-29 21:41 -0400
References <12p10atdufggrba3e5k9ie7kla17thh1ha@4ax.com>
Newsgroups comp.lang.python
Message-ID <mailman.13635.1409362912.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 8/29/2014 4:40 PM, Seymore4Head wrote:
> What is the logic behind that?

You should better ask, "How does that happen?".  Attend carefully and 
you should learn much.

In expressions, python replaces names with the object the name is bound 
to. In batch mode, a bare name is equivalent to 'pass'.  In interactive 
mode, a bare "name" is equivalent to "print(repr(name))".

 >>> s = 'abc'
 >>> s
'abc'
 >>> print(str(s))
abc
 >>> print(repr(s))
'abc'

This is a handy shortcut. "repr(name)" in turn, is also a shortcut.  It 
looks for the .__repr__ attribute of type(name) or one of the 
superclasses.  If there in no override in the inheritance tree, you see 
the default from object:

 >>> object()
<object object at 0x00000000001B2C70>

You have probably seen this pattern already. Now, what is the class of 
the object bound to 'help' (and 'quit' and 'exit')?

 >>> type(help)
<class '_sitebuiltins._Helper'>
 >>> type(quit)
<class '_sitebuiltins.Quitter'>
 >>> type(exit)
<class '_sitebuiltins.Quitter'>

Each of these classes has a .__repr__ method and a .__call__ method. 
The first simply echoes a fixed string regardless of input.

 >>> type(help).__repr__(object())
'Type help() for interactive help, or help(object) for help about object.'

We can play the same game with python code.

 >>> class Helper:
	def __repr__(self):
		return 'Type myhelp() for interactive help, or myhelp(object) for help 
about object.'
	def __call__(self, arg):
		help(arg)

 >>> myhelp = Helper()
 >>> myhelp
Type myhelp() for interactive help, or myhelp(object) for help about object.
 >>> myhelp(Helper)
Help on class Helper in module __main__:

class Helper(builtins.object)
  |  Methods defined here:
  |
  |  __call__(self, arg)
  |
  |  __repr__(self)
  |
  |  ----------------------------------------------------------------------
  |  Data descriptors defined here:
  |
  |  __dict__
  |      dictionary for instance variables (if defined)
  |
  |  __weakref__
  |      list of weak references to the object (if defined)


> Couldn't help do the same thing as help()?

Couldn't python stab people in the back by violating its own rules ;-?

(See the doc for what repr and hence .__repr__ are supposed to do.)

You should now understand that the interpreter has *no special 
knowledge* about help, exit, or quit.  The special behavior is built 
into their classes.  That is how Python objects work in relation to a 
few simple syntax rules, like "callable(args)" meaning 'call callable 
with args'.

 > older versions of python would allow ? as help.

If I ever knew that, I have forgotten.  The interpreter must have 
treated '?' as a special case for interactive input. I suspect help was 
changed when quit and exit were added.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Typing help brings up a screen that says type help() Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-29 16:40 -0400
  Re: Typing help brings up a screen that says type help() Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-29 17:10 -0400
  Re: Typing help brings up a screen that says type help() Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-29 17:15 -0400
    Re: Typing help brings up a screen that says type help() Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-29 18:14 -0400
  Re: Typing help brings up a screen that says type help() Chris Angelico <rosuav@gmail.com> - 2014-08-30 08:22 +1000
  Re: Typing help brings up a screen that says type help() Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-29 18:40 -0400
    Re: Typing help brings up a screen that says type help() Grant Edwards <invalid@invalid.invalid> - 2014-09-02 16:36 +0000
  Re: Typing help brings up a screen that says type help() Chris Angelico <rosuav@gmail.com> - 2014-08-30 08:54 +1000
  Re: Typing help brings up a screen that says type help() Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-29 19:02 -0400
  Re: Typing help brings up a screen that says type help() Chris Angelico <rosuav@gmail.com> - 2014-08-30 09:10 +1000
  Re: Typing help brings up a screen that says type help() Terry Reedy <tjreedy@udel.edu> - 2014-08-29 21:41 -0400
    Re: Typing help brings up a screen that says type help() Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-29 22:55 -0400
  Re: Typing help brings up a screen that says type help() Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-08-29 21:13 -0500
  Re: Typing help brings up a screen that says type help() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-30 14:46 +1000

csiph-web