diff options
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/Makefile | 46 | ||||
| -rw-r--r-- | doc/README | 10 | ||||
| -rw-r--r-- | doc/SetClass.py | 32 | ||||
| -rw-r--r-- | doc/fibo.py | 22 | ||||
| -rw-r--r-- | doc/mod.tex | 18 | ||||
| -rw-r--r-- | doc/mod1.tex | 38 | ||||
| -rw-r--r-- | doc/mod2.tex | 64 | ||||
| -rw-r--r-- | doc/mod3.tex | 10 | ||||
| -rw-r--r-- | doc/myformat.sty | 38 | ||||
| -rwxr-xr-x | doc/pytry | 22 | ||||
| -rw-r--r-- | doc/tut.tex | 304 |
11 files changed, 302 insertions, 302 deletions
diff --git a/doc/Makefile b/doc/Makefile index f2a83bf..b2cf688 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -1,47 +1,47 @@ # Makefile for Python documentation. -LATEX= latex -DVIPS= dvips -TEXPREVIEW= xdvi +LATEX= latex +DVIPS= dvips +TEXPREVIEW= xdvi -PRINT= lpr +PRINT= lpr -tut: tut.dvi - $(TEXPREVIEW) tut +tut: tut.dvi + $(TEXPREVIEW) tut -tut.dvi tut.ps: tut.toc tut.tex myformat.sty +tut.dvi tut.ps: tut.toc tut.tex myformat.sty -mod: mod.dvi - $(TEXPREVIEW) mod +mod: mod.dvi + $(TEXPREVIEW) mod -mod.dvi mod.ps: mod.toc mod.tex mod1.tex mod2.tex mod3.tex myformat.sty +mod.dvi mod.ps: mod.toc mod.tex mod1.tex mod2.tex mod3.tex myformat.sty -ALL= tut.ps mod.ps +ALL= tut.ps mod.ps -all: $(ALL) +all: $(ALL) -print: $(ALL) - $(PRINT) $(ALL) +print: $(ALL) + $(PRINT) $(ALL) clean: - rm -f *.dvi *.aux *.toc *.log *.ps core [#@,]* *~ + rm -f *.dvi *.aux *.toc *.log *.ps core [#@,]* *~ -.SUFFIXES: # Remove default suffixes +.SUFFIXES: # Remove default suffixes -.SUFFIXES: .tex .aux .toc .dvi .ps +.SUFFIXES: .tex .aux .toc .dvi .ps .tex.aux: - $(LATEX) $* + $(LATEX) $* .tex.toc: - $(LATEX) $* + $(LATEX) $* .tex.dvi: - $(LATEX) $* + $(LATEX) $* .dvi.ps: - $(DVIPS) $* >$*.ps + $(DVIPS) $* >$*.ps .tex.ps: - $(LATEX) $* - $(DVIPS) $* >$*.ps + $(LATEX) $* + $(DVIPS) $* >$*.ps @@ -4,8 +4,8 @@ started. The following are the important latex source files: - tut.tex A tutorial - mod.tex The library reference + tut.tex A tutorial + mod.tex The library reference They both read the style option file "myformat.sty". @@ -18,7 +18,7 @@ Type "make clean" to get rid of all the intermediate files produced by the latex process, and other junk files. You can just as well ignore the Makefile; all you really need is: - latex tut - latex tut - dvips tut | lpr + latex tut + latex tut + dvips tut | lpr and similar for the "mod" document. diff --git a/doc/SetClass.py b/doc/SetClass.py index 8a5fb9e..ca7ca6b 100644 --- a/doc/SetClass.py +++ b/doc/SetClass.py @@ -1,17 +1,17 @@ class Set(): - def new(self): - self.elements = [] - return self - def add(self, e): - if e not in self.elements: - self.elements.append(e) - def remove(self, e): - if e in self.elements: - for i in range(len(self.elements)): - if self.elements[i] = e: - del self.elements[i] - break - def is_element(self, e): - return e in self.elements - def size(self): - return len(self.elements) + def new(self): + self.elements = [] + return self + def add(self, e): + if e not in self.elements: + self.elements.append(e) + def remove(self, e): + if e in self.elements: + for i in range(len(self.elements)): + if self.elements[i] = e: + del self.elements[i] + break + def is_element(self, e): + return e in self.elements + def size(self): + return len(self.elements) diff --git a/doc/fibo.py b/doc/fibo.py index e8cafa8..83b8e83 100644 --- a/doc/fibo.py +++ b/doc/fibo.py @@ -1,15 +1,15 @@ # Fibonacci numbers demo -def fib(n): # write Fibonacci series up to n - a, b = 0, 1 - while b <= n: - print b, - a, b = b, a+b +def fib(n): # write Fibonacci series up to n + a, b = 0, 1 + while b <= n: + print b, + a, b = b, a+b def fib2(n): # return Fibonacci series up to n - ret = [] - a, b = 0, 1 - while b <= n: - ret.append(b) - a, b = b, a+b - return ret + ret = [] + a, b = 0, 1 + while b <= n: + ret.append(b) + a, b = b, a+b + return ret diff --git a/doc/mod.tex b/doc/mod.tex index c2d2355..cddfed7 100644 --- a/doc/mod.tex +++ b/doc/mod.tex @@ -5,7 +5,7 @@ % A command to force the text after an item to start on a new line \newcommand{\itembreak}{ - \mbox{}\\*[0mm] + \mbox{}\\*[0mm] } % A command to define a function item @@ -17,16 +17,16 @@ \itembreak } -\title{\bf - Python Library Reference \\ - (DRAFT) + itle{\bf + Python Library Reference \\ + (DRAFT) } \author{ - Guido van Rossum \\ - Dept. CST, CWI, Kruislaan 413 \\ - 1098 SJ Amsterdam, The Netherlands \\ - E-mail: {\tt [email protected]} + Guido van Rossum \\ + Dept. CST, CWI, Kruislaan 413 \\ + 1098 SJ Amsterdam, The Netherlands \\ + E-mail: {\tt [email protected]} } \begin{document} @@ -49,7 +49,7 @@ gives a more formal reference to the language. \pagebreak -\tableofcontents + ableofcontents \pagebreak diff --git a/doc/mod1.tex b/doc/mod1.tex index 2dac554..37abc50 100644 --- a/doc/mod1.tex +++ b/doc/mod1.tex @@ -101,15 +101,15 @@ Operation & Result & Notes \\ {\tt min}({\em s}) & smallest item of {\em s} & \\ {\tt max}({\em s}) & largest item of {\em s} & \\ {\em x} {\tt in} {\em s} & - true if an item of {\em s} is equal to {\em x} & \\ + true if an item of {\em s} is equal to {\em x} & \\ {\em x} {\tt not} {\tt in} {\em s} & - false if an item of {\em s} is equal to {\em x} & \\ + false if an item of {\em s} is equal to {\em x} & \\ {\em s}{\tt +}{\em t} & the concatenation of {\em s} and {\em t} & \\ {\em s}{\tt *}{\em n}, {\em n}*{\em s} & - {\em n} copies of {\em s} concatenated & (1) \\ + {\em n} copies of {\em s} concatenated & (1) \\ {\em s}[{\em i}] & {\em i}'th item of {\em s} & \\ {\em s}[{\em i}:{\em j}] & - slice of {\em s} from {\em i} to {\em j} & (2) \\ + slice of {\em s} from {\em i} to {\em j} & (2) \\ \hline \end{tabular} \end{center} @@ -143,19 +143,19 @@ The following operations are defined on mutable sequence types (where Operation & Result \\ \hline {\em s}[{\em i}] = {\em x} & - item {\em i} of {\em s} is replaced by {\em x} \\ + item {\em i} of {\em s} is replaced by {\em x} \\ {\em s}[{\em i}:{\em j}] = {\em t} & - slice of {\em s} from {\em i} to {\em j} is replaced by {\em t} \\ + slice of {\em s} from {\em i} to {\em j} is replaced by {\em t} \\ {\tt del} {\em s}[{\em i}:{\em j}] & - same as {\em s}[{\em i}:{\em j}] = [] \\ + same as {\em s}[{\em i}:{\em j}] = [] \\ {\em s}.{\tt append}({\em x}) & - same as {\em s}[{\tt len}({\em x}):{\tt len}({\em x})] = [{\em x}] \\ + same as {\em s}[{\tt len}({\em x}):{\tt len}({\em x})] = [{\em x}] \\ {\em s}.{\tt insert}({\em i}, {\em x}) & - same as {\em s}[{\em i}:{\em i}] = [{\em x}] \\ + same as {\em s}[{\em i}:{\em i}] = [{\em x}] \\ {\em s}.{\tt sort}() & - the items of {\em s} are permuted to satisfy \\ - & - $s[i] \leq s[j]$ for $i < j$\\ + the items of {\em s} are permuted to satisfy \\ + & + $s[i] \leq s[j]$ for $i < j$\\ \hline \end{tabular} \end{center} @@ -216,7 +216,7 @@ A small example using a dictionary: ['guido', 'irv', 'jack'] >>> tel.has_key('guido') 1 ->>> +>>> \end{verbatim}\ecode \subsubsection{Other Built-in Types} @@ -372,7 +372,7 @@ For example: ['sys'] >>> dir(sys) ['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout'] ->>> +>>> \end{verbatim}\ecode \funcitem{divmod}{a, b} %.br @@ -398,7 +398,7 @@ For example: (-15, -5) >>> divmod(-100, -7) (14, -2) ->>> +>>> \end{verbatim}\ecode \funcitem{eval}{s} Takes a string as argument and parses and evaluates it as a {\Python} @@ -411,7 +411,7 @@ For example: >>> x = 1 >>> eval('x+1') 2 ->>> +>>> \end{verbatim}\ecode \funcitem{exec}{s} Takes a string as argument and parses and evaluates it as a sequence of @@ -426,7 +426,7 @@ For example: >>> exec('x = x+1\n') >>> x 2 ->>> +>>> \end{verbatim}\ecode \funcitem{float}{x} Converts a number to floating point. @@ -494,7 +494,7 @@ For example: [] >>> range(1, 0) [] ->>> +>>> \end{verbatim}\ecode \funcitem{raw\_input}{s} %.br @@ -508,7 +508,7 @@ For example: >>> raw_input('Type anything: ') Type anything: Mutant Teenage Ninja Turtles 'Mutant Teenage Ninja Turtles' ->>> +>>> \end{verbatim}\ecode \funcitem{reload}{module} Causes an already imported module to be re-parsed and re-initialized. diff --git a/doc/mod2.tex b/doc/mod2.tex index 46f6d62..fca5e8a 100644 --- a/doc/mod2.tex +++ b/doc/mod2.tex @@ -87,12 +87,12 @@ They are: {\tt exp(x)}, {\tt fabs(x)}, {\tt floor(x)}, -%{\tt fmod(...)} XXX not yet +%{\tt fmod(...)} XXX not yet %{\tt frexp(...)} XXX not yet %{\tt ldexp(...)} XXX not yet {\tt log(x)}, {\tt log10(x)}, -%{\tt modf(...)} XXX not yet +%{\tt modf(...)} XXX not yet {\tt pow(x,y)}, {\tt sin(x)}, {\tt sinh(x)}, @@ -193,7 +193,7 @@ For example: '--b--' >>> s[3:4] # The subpattern 'b' ->>> +>>> \end{verbatim}\ecode \subsection{Built-in Module {\tt posix}} @@ -693,16 +693,16 @@ import stdwin from stdwinevents import * def main(): - mywin = stdwin.open('Hello') - # - while 1: - (type, win, detail) = stdwin.getevent() - if type = WE_DRAW: - draw = win.begindrawing() - draw.text((0, 0), 'Hello, world') - del draw - elif type = WE_CLOSE: - break + mywin = stdwin.open('Hello') + # + while 1: + (type, win, detail) = stdwin.getevent() + if type = WE_DRAW: + draw = win.begindrawing() + draw.text((0, 0), 'Hello, world') + del draw + elif type = WE_CLOSE: + break main() \end{verbatim}\ecode @@ -780,7 +780,7 @@ For example: \bcode\begin{verbatim} >>> amoeba.name_lookup('/profile/cap') aa:1c:95:52:6a:fa/14(ff)/8e:ba:5b:8:11:1a ->>> +>>> \end{verbatim}\ecode The following methods are defined for capability objects. \begin{description} @@ -879,7 +879,7 @@ implemented in C for efficiency: Amplifies a chunk of samples by a variable factor changing from {\tt f1}/256 to {\tt f2}/256. Negative factors are allowed. -Resulting values that are to large to fit in a byte are clipped. +Resulting values that are to large to fit in a byte are clipped. \funcitem{reverse}{buf} %.br Returns a chunk of samples backwards. @@ -1000,7 +1000,7 @@ is called for the normal, and then {\tt v3f()} is called for the point. \funcitem{vnarray}{} -Similar to +Similar to {\tt nvarray()} but the pairs have the point first and the normal second. \funcitem{nurbssurface}{s\_k[], t\_k[], ctl[][], s\_ord, t\_ord, type} @@ -1042,22 +1042,22 @@ Here is a tiny but complete example GL program in {\Python}: import gl, GL, time def main(): - gl.foreground() - gl.prefposition(500, 900, 500, 900) - w = gl.winopen('CrissCross') - gl.ortho2(0.0, 400.0, 0.0, 400.0) - gl.color(GL.WHITE) - gl.clear() - gl.color(GL.RED) - gl.bgnline() - gl.v2f(0.0, 0.0) - gl.v2f(400.0, 400.0) - gl.endline() - gl.bgnline() - gl.v2f(400.0, 0.0) - gl.v2f(0.0, 400.0) - gl.endline() - time.sleep(5) + gl.foreground() + gl.prefposition(500, 900, 500, 900) + w = gl.winopen('CrissCross') + gl.ortho2(0.0, 400.0, 0.0, 400.0) + gl.color(GL.WHITE) + gl.clear() + gl.color(GL.RED) + gl.bgnline() + gl.v2f(0.0, 0.0) + gl.v2f(400.0, 400.0) + gl.endline() + gl.bgnline() + gl.v2f(400.0, 0.0) + gl.v2f(0.0, 400.0) + gl.endline() + time.sleep(5) main() \end{verbatim}\ecode diff --git a/doc/mod3.tex b/doc/mod3.tex index d1a99b1..d37956c 100644 --- a/doc/mod3.tex +++ b/doc/mod3.tex @@ -19,7 +19,7 @@ The string The string {\tt '0123456789abcdefABCDEF'}. \funcitem{letters} -The concatenation of the strings +The concatenation of the strings {\tt lowercase} and {\tt uppercase} @@ -99,7 +99,7 @@ Removes leading and trailing whitespace from the string Converts lower case letters to upper case and vice versa. \funcitem{upper}{s} Convert letters to upper case. -\funcitem{ljust(s, width), rjust(s, width), center}{s, width} +\funcitem{ljust(s, width), rjust(s, width), center}{s, width} %.br These functions respectively left-justify, right-justify and center a string in a field of given width. @@ -252,7 +252,7 @@ Example: [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] >>> args ['a1', 'a2'] ->>> +>>> \end{verbatim}\ecode The exception {\tt getopt.error = 'getopt error'} @@ -305,7 +305,7 @@ Read the file for details. Suggested usage is \bcode\begin{verbatim} >>> from stdwinevents import * ->>> +>>> \end{verbatim}\ecode \subsection{Standard Module {\tt rect}} @@ -342,7 +342,7 @@ This makes it possible to quickly check whether a result is empty: >>> r3 = rect.intersect(r1, r2) >>> if r3 is rect.empty: print 'Empty intersection' Empty intersection ->>> +>>> \end{verbatim}\ecode \funcitem{is\_empty}{r} %.br diff --git a/doc/myformat.sty b/doc/myformat.sty index be70239..192ddb1 100644 --- a/doc/myformat.sty +++ b/doc/myformat.sty @@ -1,34 +1,34 @@ % Style parameters and macros used by all documents here % Page lay-out parameters -\textwidth = 160mm -\textheight = 240mm -\topmargin = -11mm -\oddsidemargin = 0mm -\evensidemargin = 0mm -%\parindent = 0mm + extwidth = 160mm + extheight = 240mm + opmargin = -11mm +\oddsidemargin = 0mm +\evensidemargin = 0mm +%\parindent = 0mm % Frequently used system names -\newcommand{\Python}{Python} % Sometimes I want this italicized +\newcommand{\Python}{Python} % Sometimes I want this italicized \newcommand{\UNIX}{U{\sc nix}} % Variable used by begin code command \newlength{\codewidth} \newcommand{\bcode}{ - % Calculate the text width for the minipage: - \setlength{\codewidth}{\linewidth} - \addtolength{\codewidth}{-\parindent} - % - \vspace{3mm} - \par - \indent - \begin{minipage}[t]{\codewidth} + % Calculate the text width for the minipage: + \setlength{\codewidth}{\linewidth} + \addtolength{\codewidth}{-\parindent} + % + \vspace{3mm} + \par + \indent + \begin{minipage}[t]{\codewidth} } \newcommand{\ecode}{ - \end{minipage} - \vspace{3mm} - \par - \noindent + \end{minipage} + \vspace{3mm} + \par + \noindent } @@ -4,17 +4,17 @@ trap 'rm -f $TMP; exit 1' 1 2 3 13 14 15 cat $* >$TMP ( - cat $TMP - - sed ' - s/^>>> // - s/^>>>$// - s/^\.\.\. // - s/^\.\.\.$// - ' $TMP | - python - - echo '>>> ' + cat $TMP + + sed ' + s/^>>> // + s/^>>>$// + s/^\.\.\. // + s/^\.\.\.$// + ' $TMP | + python + + echo '>>> ' ) 2>&1 rm $TMP diff --git a/doc/tut.tex b/doc/tut.tex index ad5f56c..e0229b9 100644 --- a/doc/tut.tex +++ b/doc/tut.tex @@ -9,10 +9,10 @@ } \author{ - Guido van Rossum \\ - Dept. CST, CWI, Kruislaan 413 \\ - 1098 SJ Amsterdam, The Netherlands \\ - E-mail: {\tt [email protected]} + Guido van Rossum \\ + Dept. CST, CWI, Kruislaan 413 \\ + 1098 SJ Amsterdam, The Netherlands \\ + E-mail: {\tt [email protected]} } \begin{document} @@ -154,15 +154,15 @@ installation option, other places instead of are possible; check with your local \Python\ guru or system administrator.% \footnote{ - At CWI, at the time of writing, the interpreter can be found in - the following places: - On the Amoeba Ultrix machines, use the standard path, - {\tt /usr/local/python}. - On the Sun file servers, use - {\tt /ufs/guido/bin/}{\em arch}{\tt /python}, - where {\em arch} can be {\tt sgi} or {\tt sun4}. - On piring, use {\tt /userfs3/amoeba/bin/python}. - (If you can't find a binary advertised here, get in touch with me.) + At CWI, at the time of writing, the interpreter can be found in + the following places: + On the Amoeba Ultrix machines, use the standard path, + {\tt /usr/local/python}. + On the Sun file servers, use + {\tt /ufs/guido/bin/}{\em arch}{\tt /python}, + where {\em arch} can be {\tt sgi} or {\tt sun4}. + On piring, use {\tt /userfs3/amoeba/bin/python}. + (If you can't find a binary advertised here, get in touch with me.) } The interpreter operates somewhat like the \UNIX\ shell: when called with @@ -172,13 +172,13 @@ standard input, it reads and executes a {\em script} from that file.% \footnote{ - There is a difference between ``{\tt python file}'' and - ``{\tt python $<$file}''. In the latter case {\tt input()} and - {\tt raw\_input()} are satisfied from {\em file}, which has - already been read until the end by the parser, so they will read - EOF immediately. In the former case (which is usually what - you want) they are satisfied from whatever file or device is - connected to standard input of the \Python\ interpreter. + There is a difference between ``{\tt python file}'' and + ``{\tt python $<$file}''. In the latter case {\tt input()} and + {\tt raw\_input()} are satisfied from {\em file}, which has + already been read until the end by the parser, so they will read + EOF immediately. In the former case (which is usually what + you want) they are satisfied from whatever file or device is + connected to standard input of the \Python\ interpreter. } If available, the script name and additional arguments thereafter are passed to the script in the variable @@ -232,10 +232,10 @@ When is not set, an installation-dependent default path is used, usually {\tt .:/usr/local/lib/python}.% \footnote{ - Modules are really searched in the list of directories given by - the variable {\tt sys.path} which is initialized from - {\tt PYTHONPATH} or from the installation-dependent default. - See the section on Standard Modules later. + Modules are really searched in the list of directories given by + the variable {\tt sys.path} which is initialized from + {\tt PYTHONPATH} or from the installation-dependent default. + See the section on Standard Modules later. } On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable, @@ -262,10 +262,10 @@ however, the basics are easily explained. If supported,% \footnote{ - Perhaps the quickest check to see whether command line editing - is supported is typing Control-P to the first \Python\ prompt - you get. If it beeps, you have command line editing. - If not, you can skip the rest of this section. + Perhaps the quickest check to see whether command line editing + is supported is typing Control-P to the first \Python\ prompt + you get. If it beeps, you have command line editing. + If not, you can skip the rest of this section. } input line editing is active whenever the interpreter prints a primary or secondary prompt. @@ -423,9 +423,9 @@ notation: two subscripts (indices) separated by a colon. >>> word[2:4] 'lp' >>> # Slice indices have useful defaults: ->>> word[:2] # Take first two characters +>>> word[:2] # Take first two characters 'He' ->>> word[2:] # Drop first two characters +>>> word[2:] # Drop first two characters 'lpA' >>> # A useful invariant: s[:i] + s[i:] = s >>> word[:3] + word[3:] @@ -448,12 +448,12 @@ Slice indices (but not simple subscripts) may be negative numbers, to start counting from the right. For example: \bcode\begin{verbatim} ->>> word[-2:] # Take last two characters +>>> word[-2:] # Take last two characters 'pA' ->>> word[:-2] # Drop last two characters +>>> word[:-2] # Drop last two characters 'Hel' >>> # But -0 does not count from the right! ->>> word[-0:] # (since -0 equals 0) +>>> word[-0:] # (since -0 equals 0) 'HelpA' >>> \end{verbatim}\ecode @@ -470,8 +470,8 @@ for example: +---+---+---+---+---+ | H | e | l | p | A | +---+---+---+---+---+ - 0 1 2 3 4 5 --5 -4 -3 -2 -1 + 0 1 2 3 4 5 +-5 -4 -3 -2 -1 \end{verbatim}\ecode The first row of numbers gives the position of the indices 0...5 in the string; the second row gives the corresponding negative indices. @@ -570,8 +570,8 @@ series as follows: >>> # the sum of two elements defines the next >>> a, b = 0, 1 >>> while b < 100: -... print b -... a, b = b, a+b +... print b +... a, b = b, a+b ... 1 1 @@ -616,9 +616,9 @@ The standard comparison operators are written as and {\tt <>}.% \footnote{ - The ambiguity of using {\tt =} - for both assignment and equality is resolved by disallowing - unparenthesized conditions at the right hand side of assignments. + The ambiguity of using {\tt =} + for both assignment and equality is resolved by disallowing + unparenthesized conditions at the right hand side of assignments. } \item The @@ -651,8 +651,8 @@ A trailing comma avoids the newline after the output: \bcode\begin{verbatim} >>> a, b = 0, 1 >>> while b < 1000: -... print b, -... a, b = b, a+b +... print b, +... a, b = b, a+b ... 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> @@ -673,14 +673,14 @@ Perhaps the most well-known statement type is the {\tt if} statement. For example: \bcode\begin{verbatim} >>> if x < 0: -... x = 0 -... print 'Negative changed to zero' +... x = 0 +... print 'Negative changed to zero' ... elif x = 0: -... print 'Zero' +... print 'Zero' ... elif x = 1: -... print 'Single' +... print 'Single' ... else: -... print 'More' +... print 'More' ... \end{verbatim}\ecode There can be zero or more {\tt elif} parts, and the {\tt else} part is @@ -703,7 +703,7 @@ For example (no pun intended): >>> # Measure some strings: >>> a = ['cat', 'window', 'defenestrate'] >>> for x in a: -... print x, len(x) +... print x, len(x) ... cat 3 window 6 @@ -741,7 +741,7 @@ and {\tt len()} as follows: \bcode\begin{verbatim} >>> a = ['Mary', 'had', 'a', 'little', 'boy'] >>> for i in range(len(a)): -... print i, a[i] +... print i, a[i] ... 0 Mary 1 had @@ -763,12 +763,12 @@ This is exemplified by the following loop, which searches for a list item of value 0: \bcode\begin{verbatim} >>> for n in range(2, 10): -... for x in range(2, n): -... if n % x = 0: -... print n, 'equals', x, '*', n/x -... break -... else: -... print n, 'is a prime number' +... for x in range(2, n): +... if n % x = 0: +... print n, 'equals', x, '*', n/x +... break +... else: +... print n, 'is a prime number' ... 2 is a prime number 3 is a prime number @@ -789,7 +789,7 @@ program requires no action. For example: \bcode\begin{verbatim} >>> while 1: -... pass # Busy-wait for keyboard interrupt +... pass # Busy-wait for keyboard interrupt ... \end{verbatim}\ecode @@ -802,11 +802,11 @@ XXX To Be Done. We can create a function that writes the Fibonacci series to an arbitrary boundary: \bcode\begin{verbatim} ->>> def fib(n): # write Fibonacci series up to n -... a, b = 0, 1 -... while b <= n: -... print b, -... a, b = b, a+b +>>> def fib(n): # write Fibonacci series up to n +... a, b = 0, 1 +... while b <= n: +... print b, +... a, b = b, a+b ... >>> # Now call the function we just defined: >>> fib(2000) @@ -837,10 +837,10 @@ the local symbol table of the called function when it is called; thus, arguments are passed using {\em call\ by\ value}.% \footnote{ - Actually, {\em call by object reference} would be a better - description, since if a mutable object is passed, the caller - will see any changes the callee makes to it (e.g., items - inserted into a list). + Actually, {\em call by object reference} would be a better + description, since if a mutable object is passed, the caller + will see any changes the callee makes to it (e.g., items + inserted into a list). } When a function calls another function, a new local symbol table is created for that call. @@ -880,15 +880,15 @@ It is simple to write a function that returns a list of the numbers of the Fibonacci series, instead of printing it: \bcode\begin{verbatim} >>> def fib2(n): # return Fibonacci series up to n -... result = [] -... a, b = 0, 1 -... while b <= n: -... result.append(b) # see below -... a, b = b, a+b -... return result +... result = [] +... a, b = 0, 1 +... while b <= n: +... result.append(b) # see below +... a, b = b, a+b +... return result ... ->>> f100 = fib2(100) # call it ->>> f100 # write the result +>>> f100 = fib2(100) # call it +>>> f100 # write the result [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> \end{verbatim}\ecode @@ -928,8 +928,8 @@ In this case it is equivalent to {\tt ret = ret + [b]}, but more efficient.% \footnote{ - There is a subtle semantic difference if the object - is referenced from more than one place. + There is a subtle semantic difference if the object + is referenced from more than one place. } \end{itemize} The list object type has two more methods: @@ -993,19 +993,19 @@ in the current directory with the following contents: \bcode\begin{verbatim} # Fibonacci numbers module -def fib(n): # write Fibonacci series up to n - a, b = 0, 1 - while b <= n: - print b, - a, b = b, a+b +def fib(n): # write Fibonacci series up to n + a, b = 0, 1 + while b <= n: + print b, + a, b = b, a+b def fib2(n): # return Fibonacci series up to n - ret = [] - a, b = 0, 1 - while b <= n: - ret.append(b) - a, b = b, a+b - return ret + ret = [] + a, b = 0, 1 + while b <= n: + ret.append(b) + a, b = b, a+b + return ret \end{verbatim}\ecode Now enter the \Python\ interpreter and import this module with the following command: @@ -1043,9 +1043,9 @@ They are executed only the {\em first} time the module is imported somewhere.% \footnote{ - In fact function definitions are also `statements' that are - `executed'; the execution enters the function name in the - module's global symbol table. + In fact function definitions are also `statements' that are + `executed'; the execution enters the function name in the + module's global symbol table. } Each module has its own private symbol table, which is used as the @@ -1157,7 +1157,7 @@ kind of complaint you get while you are still learning \Python: >>> while 1 print 'Hello world' Parsing error: file <stdin>, line 1: while 1 print 'Hello world' - ^ + ^ Unhandled exception: run-time error: syntax error >>> \end{verbatim}\ecode @@ -1178,15 +1178,15 @@ an error when an attempt is made to execute it: >>> 10 * (1/0) Unhandled exception: run-time error: integer division by zero Stack backtrace (innermost last): - File "<stdin>", line 1 + File "<stdin>", line 1 >>> 4 + foo*3 Unhandled exception: undefined name: foo Stack backtrace (innermost last): - File "<stdin>", line 1 + File "<stdin>", line 1 >>> '2' + 2 Unhandled exception: type error: illegal argument type for built-in operation Stack backtrace (innermost last): - File "<stdin>", line 1 + File "<stdin>", line 1 >>> \end{verbatim}\ecode Errors detected during execution are called @@ -1223,9 +1223,9 @@ The detail states the cause of the error in more detail. errors are more serious: these are usually caused by misspelled identifiers.% \footnote{ - The parser does not check whether names used in a program are at - all defined elsewhere in the program, so such checks are - postponed until run-time. The same holds for type checking. + The parser does not check whether names used in a program are at + all defined elsewhere in the program, so such checks are + postponed until run-time. The same holds for type checking. } The detail is the offending identifier. \item @@ -1244,11 +1244,11 @@ some floating point numbers: \bcode\begin{verbatim} >>> numbers = [0.3333, 2.5, 0.0, 10.0] >>> for x in numbers: -... print x, -... try: -... print 1.0 / x -... except RuntimeError: -... print '*** has no inverse ***' +... print x, +... try: +... print 1.0 / x +... except RuntimeError: +... print '*** has no inverse ***' ... 0.3333 3.00030003 2.5 0.4 @@ -1288,7 +1288,7 @@ An except clause may name multiple exceptions as a parenthesized list, e.g.: \bcode\begin{verbatim} ... except (RuntimeError, TypeError, NameError): -... pass +... pass \end{verbatim}\ecode The last except clause may omit the exception name(s), to serve as a wildcard. @@ -1303,9 +1303,9 @@ specify a variable after the exception name (or list) to receive the argument's value, as follows: \bcode\begin{verbatim} >>> try: -... foo() +... foo() ... except NameError, x: -... print 'name', x, 'undefined' +... print 'name', x, 'undefined' ... name foo undefined >>> @@ -1319,21 +1319,21 @@ These are in fact string objects whose {\em object\ identity} (not their value!) identifies the exceptions.% \footnote{ - There should really be a separate exception type; it is pure - laziness that exceptions are identified by strings, and this may - be fixed in the future. + There should really be a separate exception type; it is pure + laziness that exceptions are identified by strings, and this may + be fixed in the future. } The string is printed as the second part of the message for unhandled exceptions. Their names and values are: \bcode\begin{verbatim} -EOFError 'end-of-file read' -KeyboardInterrupt 'keyboard interrupt' -MemoryError 'out of memory' * -NameError 'undefined name' * -RuntimeError 'run-time error' * -SystemError 'system error' * -TypeError 'type error' * +EOFError 'end-of-file read' +KeyboardInterrupt 'keyboard interrupt' +MemoryError 'out of memory' * +NameError 'undefined name' * +RuntimeError 'run-time error' * +SystemError 'system error' * +TypeError 'type error' * \end{verbatim}\ecode The meanings should be clear enough. Those exceptions with a {\tt *} in the third column have an argument. @@ -1344,12 +1344,12 @@ that are called (even indirectly) in the try clause. For example: \bcode\begin{verbatim} >>> def this_fails(): -... x = 1/0 +... x = 1/0 ... >>> try: -... this_fails() +... this_fails() ... except RuntimeError, detail: -... print 'Handling run-time error:', detail +... print 'Handling run-time error:', detail ... Handling run-time error: domain error or zero division >>> @@ -1364,7 +1364,7 @@ For example: >>> raise NameError, 'Hi There!' Unhandled exception: undefined name: Hi There! Stack backtrace (innermost last): - File "<stdin>", line 1 + File "<stdin>", line 1 >>> \end{verbatim}\ecode The first argument to {\tt raise} names the exception to be raised. @@ -1378,15 +1378,15 @@ For example: \bcode\begin{verbatim} >>> my_exc = 'nobody likes me!' >>> try: -... raise my_exc, 2*2 +... raise my_exc, 2*2 ... except my_exc, val: -... print 'My exception occured, value:', val +... print 'My exception occured, value:', val ... My exception occured, value: 4 >>> raise my_exc, 1 Unhandled exception: nobody likes me!: 1 Stack backtrace (innermost last): - File "<stdin>", line 7 + File "<stdin>", line 7 >>> \end{verbatim}\ecode Many standard modules use this to report errors that may occur in @@ -1399,14 +1399,14 @@ define clean-up actions that must be executed under all circumstances. For example: \bcode\begin{verbatim} >>> try: -... raise KeyboardInterrupt +... raise KeyboardInterrupt ... finally: -... print 'Goodbye, world!' +... print 'Goodbye, world!' ... Goodbye, world! Unhandled exception: keyboard interrupt Stack backtrace (innermost last): - File "<stdin>", line 2 + File "<stdin>", line 2 >>> \end{verbatim}\ecode The @@ -1456,11 +1456,11 @@ Also, like Modula-3 but unlike C++, the built-in operators with special syntax (arithmetic operators, subscripting etc.) cannot be redefined for class members.% \footnote{ - They can be redefined for new object types implemented in C in - extensions to the interpreter, however. It would require only a - naming convention and a relatively small change to the - interpreter to allow operator overloading for classes, so - perhaps someday... + They can be redefined for new object types implemented in C in + extensions to the interpreter, however. It would require only a + naming convention and a relatively small change to the + interpreter to allow operator overloading for classes, so + perhaps someday... } \subsubsection{A Simple Example} @@ -1471,22 +1471,22 @@ remove elements, a membership test, and a request for the size of the set. \bcode\begin{verbatim} class Set(): - def new(self): - self.elements = [] - return self - def add(self, e): - if e not in self.elements: - self.elements.append(e) - def remove(self, e): - if e in self.elements: - for i in range(len(self.elements)): - if self.elements[i] = e: - del self.elements[i] - break - def is_element(self, e): - return e in self.elements - def size(self): - return len(self.elements) + def new(self): + self.elements = [] + return self + def add(self, e): + if e not in self.elements: + self.elements.append(e) + def remove(self, e): + if e in self.elements: + for i in range(len(self.elements)): + if self.elements[i] = e: + del self.elements[i] + break + def is_element(self, e): + return e in self.elements + def size(self): + return len(self.elements) \end{verbatim}\ecode Note that the class definition looks like a big compound statement, with all the function definitons indented repective to the @@ -1538,13 +1538,13 @@ XXX This section is not complete yet! \section{XXX P.M.} \begin{itemize} -\item The {\tt del} statement. -\item The {\tt dir()} function. -\item Tuples. -\item Dictionaries. -\item Objects and types in general. -\item Backquotes. -\item And/Or/Not. +\item The {\tt del} statement. +\item The {\tt dir()} function. +\item Tuples. +\item Dictionaries. +\item Objects and types in general. +\item Backquotes. +\item And/Or/Not. \end{itemize} \end{document} |
