Yet another newbie question - standard or built-in methods

Bengt Richter bokr at oz.net
Fri Dec 5 11:10:05 EST 2003


On Fri, 05 Dec 2003 13:51:14 +0100, Peter Otten <__peter__ at web.de> wrote:

>Kamus of Kadizhar wrote:
>
>> OK, I've been playing with the snippets of code posted earlier.
>> 
>> Among them are things like
>> 
>> allmovies[movie] = allmovies.get(movie, 0) + 1
>> 
>>  From reading the docs, I've gathered that the '.get(xxx,yyy)' part is a
>> method that operates on the dictonary allmovies.  (Sorry if I have the
>> terminology wrong).
>> 
>> But nowhere can I find a list of 'standard' or 'built-in' methods, or
>> methods that can be used with various variable classes.  What exactly
>> does 'get' do?  This seems to be so basic to python that it's not
>> explained anywhere, but that's no help to me.... Is there a list with
>> explanations somewhere?  I've been through the tutorials and guides, and
>> all just start using these with no explanation of what they do and how
>> they work.
>> 
>> Maybe I've missed it somewhere; just point me to the right FM, so I can
>> RTFM.
>> 
>> help is no help:
>> 
>> help> get
>> no Python documentation found for 'get'
>> 
>> So what's a newbie to do?
>
>Raymond Hettinger has already pointed you to the relevant part of the
>documentation. I will add that help *is* helpful. You just need to provide
>some context:
>
>>>> allmovies = {}
>>>> help(allmovies.get)
>
>will show you a short explanation of the dict.get() method. 
>Unfortunately help({}) does not come up with the help for dictionaries, but
>it does mention the class (dict), so that the next step should work to your
>satisfaction:
>
>>>> help(allmovies) # not very informative
>
>>>> help(dict) # nice
>
>help() has the additional benefit of working for third party (or your own)
>code that provides docstrings:
>
>>>> def foo(bar):
>...     "Reliably foo any bar you care to mention"
>...
>>>> help(foo)
>
>Or, more likely:
>
>>>> import otten.statistics 
>>>> help(otten.statistics) # and old module, hope I threw in some doc :-)
>
There is also the interactive help, but it seems not as useful in one way,
i.e., it does not seem to recognize dotted subjects:

 >>> help()

 Welcome to Python 2.3!  This is the online help utility.

 If this is your first time using Python, you should definitely check out
 the tutorial on the Internet at http://www.python.org/doc/tut/.

 Enter the name of any module, keyword, or topic to get help on writing
 Python programs and using Python modules.  To quit this help utility and
 return to the interpreter, just type "quit".

 To get a list of available modules, keywords, or topics, type "modules",
 "keywords", or "topics".  Each module also comes with a one-line summary
 of what it does; to list the modules whose summaries contain a given word
 such as "spam", type "modules spam".

 help> get
 no Python documentation found for 'get'

 help> dict.get
 no Python documentation found for 'dict.get'

 help> ^Z

 You are now leaving help and returning to the Python interpreter.
 If you want to ask for help on a particular object directly from the
 interpreter, you can type "help(object)".  Executing "help('string')"
 has the same effect as typing a particular string at the help> prompt.

whereas

 >>> help(dict.get)
 Help on method_descriptor:

 get(...)
     D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.


NB: Before you run interactive help on a totally unknown source, remember that
it is not a passive inspection of the text -- it imports, and thus executes code:

 >>> print """
 ... ====< unsafe.py >====
 ... %s====================="""%file('unsafe.py').read()

 ====< unsafe.py >====
 # unsafe.py
 """Show unsafe execution by interactive help through import"""
 def boxit(s):
     """Return string with ascii box around it"""
     width =max(map(len, s.splitlines())); bar = ['+-%s-+'%('-'*width)]
     def padded(s, w=width): return s + (len(s)<w and ' '*(w-len(s)) or '' )
     return '\n'.join(bar+ ['| %s |'%padded(line) for line in s.splitlines()] +bar)

 print boxit('This could be\nany unsafe action!')
 
 =====================
 >>> help('unsafe')
 +--------------------+
 | This could be      |
 | any unsafe action! |
 +--------------------+
 Help on module unsafe: 

 NAME
     unsafe - Show unsafe execution by interactive help through import

 FILE
     c:\pywk\clp\unsafe.py

 FUNCTIONS
     boxit(s)
         Return string with ascii box around it

Also note that in the same session, repeated help will not re-execute module texts,
since import accesses previously imported modules when available.

Regards,
Bengt Richter




More information about the Python-list mailing list