lecture04

lecture04 - Lecture 4 October 6 2010 Lists etc Caltech CS 1...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 4 : October 6, 2010 Lists, etc.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Objects Method syntax (dot syntax) Modules Module syntax (dot syntax)
Background image of page 2
Caltech CS 1: Fall 2010 Documentation (docstrings) The __main__ module Lists
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 We've talked about comments already: # This is a comment. Very common use of comments is to describe what a function does: # Print out a hearty greeting. def greet(name): print "Hi there, %s" % name
Background image of page 4
Caltech CS 1: Fall 2010 Comments describing the purpose of functions are very useful However, Python's help() function can't use them It would be cool if there were a special way to write comments so that they could be used by the help() function And there is! It's called a docstring short for "documentation string"
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 A docstring is just a regular Python string that is the first thing in any of: a function body a module a class (later in course) When executing a function body, the docstring doesn't do anything but Python stores it as part of the function same is true for modules and classes
Background image of page 6
Caltech CS 1: Fall 2010 greet function from last time, with a docstring: def greet(name): '''Print out a hearty greeting.''' print "Hi there, %s" % name Docstrings usually written using triple-quoted strings (multiline strings) because docstrings often span more than 1 line docstring
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Assume this is still part of module greetings Now can do this: >>> help(greetings.greet) Help on function greet in module greetings: greet(name) Print out a hearty greeting.
Background image of page 8
Caltech CS 1: Fall 2010 Similarly, can have docstrings for entire module New version of file greetings.py : '''Module: greetings Functions to print out greetings.''' def greet(name): '''Print out a hearty greeting.''' print "Hi there, %s!" % name def insult(name): '''Print out a nasty insult.''' print "Get lost, %s!" % name module docstring
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 >>> help(greetings) FILE greetings.py DESCRIPTION Module: greetings Functions to print out greetings. FUNCTIONS greet(name) Print out a hearty greeting. insult(name) Print out a nasty insult.
Background image of page 10
Caltech CS 1: Fall 2010 We will expect you to write docstrings for all your functions and modules Docstrings are good documentation for you for you in the future for anyone else that wants to use your modules/ functions
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
For functions , a docstring should describe 1. what the function does 2. what the function arguments represent 3. what the function return value represents For modules , a docstring should describe the purpose of the module general description of the kinds of functions in the module but not a detailed description of the functions! any other relevant information
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

Page1 / 68

lecture04 - Lecture 4 October 6 2010 Lists etc Caltech CS 1...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online