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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'interpreter': 0.05; 'that?': 0.05; 'attribute': 0.07; 'method.': 0.07; 'variables': 0.07; 'subject:help': 0.08; 'string': 0.09; 'already.': 0.09; 'classes.': 0.09; 'exit': 0.09; 'logic': 0.09; 'override': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:()': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; "'type": 0.16; 'added.': 0.16; 'arg):': 0.16; 'callable': 0.16; 'descriptors': 0.16; 'expressions,': 0.16; 'inheritance': 0.16; 'mode,': 0.16; 'object()': 0.16; 'quit.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'repr': 0.16; 'subject:screen': 0.16; 'subject:type': 0.16; 'turn,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'help.': 0.21; '>>>': 0.22; 'rules': 0.22; 'header:User-Agent:1': 0.23; 'helper': 0.24; 'regardless': 0.24; 'versions': 0.24; 'looks': 0.24; '(see': 0.26; 'equivalent': 0.26; 'references': 0.26; 'defined': 0.27; 'skip:" 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'attend': 0.31; 'doc': 0.31; 'object.': 0.31; 'subject:that': 0.31; 'class': 0.32; 'probably': 0.32; 'supposed': 0.32; 'older': 0.33; 'skip:_ 10': 0.34; 'classes': 0.35; 'objects': 0.35; 'there': 0.35; 'much.': 0.36; 'method': 0.36; 'should': 0.36; 'behind': 0.37; 'list': 0.37; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'does': 0.39; "couldn't": 0.39; 'help,': 0.39; 'received:71': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'how': 0.40; 'simply': 0.61; 'simple': 0.61; 'first': 0.61; 'here:': 0.62; 'back': 0.62; 'name': 0.63; 'default': 0.69; 'carefully': 0.74; 'special': 0.74; 'behavior': 0.77; '*no': 0.84; 'abc': 0.84; 'bare': 0.84; 'object:': 0.84; 'received:fios.verizon.net': 0.84; '"how': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Typing help brings up a screen that says type help()
Date Fri, 29 Aug 2014 21:41:30 -0400
References <12p10atdufggrba3e5k9ie7kla17thh1ha@4ax.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-71-175-90-87.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <12p10atdufggrba3e5k9ie7kla17thh1ha@4ax.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13635.1409362912.18130.python-list@python.org> (permalink)
Lines 95
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409362912 news.xs4all.nl 2927 [2001:888:2000:d::a6]:38414
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77307

Show key headers only | 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