diff options
author | Guido van Rossum <guido@python.org> | 1994-08-01 12:22:53 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1994-08-01 12:22:53 (GMT) |
commit | 6938f06047a6d2170523cfc3ab8e797bae0a6c05 (patch) | |
tree | 1da91a88f322e85f03c8eedfc191f610209dfb4e /Doc/tut | |
parent | ab3a2504b97d5131779f400717dc491919783dd0 (diff) | |
download | cpython-6938f06047a6d2170523cfc3ab8e797bae0a6c05.zip cpython-6938f06047a6d2170523cfc3ab8e797bae0a6c05.tar.gz cpython-6938f06047a6d2170523cfc3ab8e797bae0a6c05.tar.bz2 |
Merge alpha100 branch back to main trunk
Diffstat (limited to 'Doc/tut')
-rw-r--r-- | Doc/tut/tut.tex | 743 |
1 files changed, 618 insertions, 125 deletions
diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex index 5327488..5353d56 100644 --- a/Doc/tut/tut.tex +++ b/Doc/tut/tut.tex @@ -1,17 +1,15 @@ \documentstyle[twoside,11pt,myformat]{report} -\title{\bf - Python Tutorial -} - +\title{Python Tutorial} + \author{ - Guido van Rossum \\ - Dept. CST, CWI, P.O. Box 94079 \\ - 1090 GB Amsterdam, The Netherlands \\ - E-mail: {\tt guido@cwi.nl} + Guido van Rossum \\ + Dept. CST, CWI, P.O. Box 94079 \\ + 1090 GB Amsterdam, The Netherlands \\ + E-mail: {\tt guido@cwi.nl} } -\date{19 November 1993 \\ Release 0.9.9.++} % XXX update before release! +\date{14 Jul 1994 \\ Release 1.0.3} % XXX update before release! \begin{document} @@ -65,7 +63,7 @@ a more formal definition of the language. If you ever wrote a large shell script, you probably know this feeling: you'd love to add yet another feature, but it's already so slow, and so big, and so complicated; or the feature involves a system -call or other funcion that is only accessible from C \ldots Usually +call or other function that is only accessible from C \ldots Usually the problem at hand isn't serious enough to warrant rewriting the script in C; perhaps because the problem requires variable-length strings or other data types (like sorted lists of file names) that are @@ -137,9 +135,10 @@ trying out the examples shown later. The rest of the tutorial introduces various features of the Python language and system though examples, beginning with simple expressions, statements and data types, through functions and modules, -and finally touching upon advanced concepts like exceptions. +and finally touching upon advanced concepts like exceptions +and user-defined classes. -When you're through with the turtorial (or just getting bored), you +When you're through with the tutorial (or just getting bored), you should read the Library Reference, which gives complete (though terse) reference material about built-in and standard types, functions and modules that can save you a lot of time when writing Python programs. @@ -219,8 +218,8 @@ and a copyright notice before printing the first prompt, e.g.: \bcode\begin{verbatim} python -Python 0.9.9 (Apr 2 1993). -Copyright 1990, 1991, 1992, 1993 Stichting Mathematisch Centrum, Amsterdam +Python 1.0.3 (Jul 14 1994) +Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam >>> \end{verbatim}\ecode @@ -244,7 +243,7 @@ Typing the interrupt character (usually Control-C or DEL) to the primary or secondary prompt cancels the input and returns to the primary prompt.% \footnote{ - A problem with the GNU Readline package may prevent this. + A problem with the GNU Readline package may prevent this. } Typing an interrupt while a command is executing raises the {\tt KeyboardInterrupt} exception, which may be handled by a {\tt try} @@ -301,7 +300,7 @@ When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. You can do this by setting an environment variable named {\tt PYTHONSTARTUP} to the name of a file containing your start-up -commands. This is similar to the {\tt /profile} feature of the UNIX +commands. This is similar to the {\tt .profile} feature of the UNIX shells. This file is only read in interactive sessions, not when Python reads @@ -423,9 +422,9 @@ the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter.% \footnote{ - I'd prefer to use different fonts to distinguish input - from output, but the amount of LaTeX hacking that would require - is currently beyond my ability. + I'd prefer to use different fonts to distinguish input + from output, but the amount of LaTeX hacking that would require + is currently beyond my ability. } Note that a secondary prompt on a line by itself in an example means you must type a blank line; this is used to end a multi-line command. @@ -444,15 +443,20 @@ work just like in most other languages (e.g., Pascal or C); parentheses can be used for grouping. For example: \bcode\begin{verbatim} ->>> # This is a comment >>> 2+2 4 ->>> +>>> # This is a comment +... 2+2 +4 +>>> 2+2 # and a comment on the same line as code +4 >>> (50-5*6)/4 5 ->>> # Division truncates towards zero: ->>> 7/3 +>>> # Integer division returns the floor: +... 7/3 2 +>>> 7/-3 +-3 >>> \end{verbatim}\ecode % @@ -470,9 +474,14 @@ variable. The value of an assignment is not written: A value can be assigned to several variables simultaneously: \bcode\begin{verbatim} ->>> # Zero x, y and z ->>> x = y = z = 0 ->>> +>>> x = y = z = 0 # Zero x, y and z +>>> x +0 +>>> y +0 +>>> z +0 +>>> \end{verbatim}\ecode % There is full support for floating point; operators with mixed type @@ -489,21 +498,30 @@ operands convert the integer operand to floating point: \subsection{Strings} Besides numbers, Python can also manipulate strings, enclosed in -single quotes: +single quotes or double quotes: \bcode\begin{verbatim} >>> 'foo bar' 'foo bar' >>> 'doesn\'t' -'doesn\'t' +"doesn't" +>>> "doesn't" +"doesn't" +>>> '"Yes," he said.' +'"Yes," he said.' +>>> "\"Yes,\" he said." +'"Yes," he said.' +>>> '"Isn\'t," she said.' +'"Isn\'t," she said.' >>> \end{verbatim}\ecode % Strings are written the same way as they are typed for input: inside -quotes and with quotes and other funny characters escaped by -backslashes, to show the precise value. (The {\tt print} statement, -described later, can be used to write strings without quotes or -escapes.) +quotes and with quotes and other funny characters escaped by backslashes, +to show the precise value. The string is enclosed in double quotes if +the string contains a single quote and no double quotes, else it's +enclosed in single quotes. (The {\tt print} statement, described later, +can be used to write strings without quotes or escapes.) Strings can be concatenated (glued together) with the {\tt +} operator, and repeated with {\tt *}: @@ -602,7 +620,9 @@ for single-element (non-slice) indices: >>> word[-100:] 'HelpA' >>> word[-10] # error -Unhandled exception: IndexError: string index out of range +Traceback (innermost last): + File "<stdin>", line 1 +IndexError: string index out of range >>> \end{verbatim}\ecode % @@ -687,15 +707,15 @@ of the list: \bcode\begin{verbatim} >>> # Replace some items: ->>> a[0:2] = [1, 12] +... a[0:2] = [1, 12] >>> a [1, 12, 123, 1234] >>> # Remove some: ->>> a[0:2] = [] +... a[0:2] = [] >>> a [123, 1234] >>> # Insert some: ->>> a[1:1] = ['bletch', 'xyzzy'] +... a[1:1] = ['bletch', 'xyzzy'] >>> a [123, 'bletch', 'xyzzy', 1234] >>> a[:0] = a # Insert (a copy of) itself at the beginning @@ -743,8 +763,8 @@ subsequence of the {\em Fibonacci} series as follows: \bcode\begin{verbatim} >>> # Fibonacci series: ->>> # the sum of two elements defines the next ->>> a, b = 0, 1 +... # the sum of two elements defines the next +... a, b = 0, 1 >>> while b < 10: ... print b ... a, b = b, a+b @@ -864,7 +884,7 @@ example (no pun intended): \bcode\begin{verbatim} >>> # Measure some strings: ->>> a = ['cat', 'window', 'defenestrate'] +... a = ['cat', 'window', 'defenestrate'] >>> for x in a: ... print x, len(x) ... @@ -992,7 +1012,7 @@ arbitrary boundary: ... a, b = b, a+b ... >>> # Now call the function we just defined: ->>> fib(2000) +... fib(2000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 >>> \end{verbatim}\ecode @@ -1006,20 +1026,21 @@ The {\em execution} of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas - variable references first look in the local symbol table, then +variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned to from within a -function, although they may be referenced. +function (unless named in a {\tt global} statement), although +they may be referenced. The actual parameters (arguments) to a function call are introduced in 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. @@ -1081,7 +1102,7 @@ This example, as usual, demonstrates some new Python features: \item The {\tt return} statement returns with a value from a function. {\tt return} without an expression argument is used to return from the middle -of a procedure (falling off the end also returns from a proceduce), in +of a procedure (falling off the end also returns from a procedure), in which case the {\tt None} value is returned. \item @@ -1092,8 +1113,8 @@ object (this may be an expression), and {\tt methodname} is the name of a method that is defined by the object's type. Different types define different methods. Methods of different types may have the same name without causing ambiguity. (It is possible to define your -own object types and methods, using {\em classes}. This is an -advanced feature that is not discussed in this tutorial.) +own object types and methods, using {\em classes}, as discussed later +in this tutorial.) The method {\tt append} shown in the example, is defined for list objects; it adds a new element at the end of the list. In this example @@ -1137,12 +1158,17 @@ Sort the items of the list, in place. \item[{\tt reverse()}] Reverse the elements of the list, in place. +\item[{\tt count(x)}] +Return the number of times {\tt x} appears in the list. + \end{description} An example that uses all list methods: \bcode\begin{verbatim} >>> a = [66.6, 333, 333, 1, 1234.5] +>>> print a.count(333), a.count(66.6), a.count('x') +2 1 0 >>> a.insert(2, -1) >>> a.append(333) >>> a @@ -1194,7 +1220,7 @@ later. \section{Tuples and Sequences} We saw that lists and strings have many common properties, e.g., -indexinging and slicing operations. They are two examples of {\em +indexing and slicing operations. They are two examples of {\em sequence} data types. Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the {\em tuple}. @@ -1209,7 +1235,7 @@ instance: >>> t (12345, 54321, 'hello!') >>> # Tuples may be nested: ->>> u = t, (1, 2, 3, 4, 5) +... u = t, (1, 2, 3, 4, 5) >>> u ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5)) >>> @@ -1227,7 +1253,7 @@ simulate much of the same effect with slicing and concatenation, though). A special problem is the construction of tuples containing 0 or 1 -items: the syntax has some extra quirks to accomodate these. Empty +items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). @@ -1277,7 +1303,9 @@ Another useful data type built into Python is the {\em dictionary}. Dictionaries are sometimes found in other languages as ``associative memories'' or ``associative arrays''. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by {\em keys}, -which are strings. It is best to think of a dictionary as an unordered set of +which are strings (the use of non-string values as keys +is supported, but beyond the scope of this tutorial). +It is best to think of a dictionary as an unordered set of {\em key:value} pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: \verb/{}/. @@ -1291,7 +1319,7 @@ a key:value pair with {\tt del}. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a -value using a non-existant key. +value using a non-existent key. The {\tt keys()} method of a dictionary object returns a list of all the keys used in the dictionary, in random order (if you want it sorted, @@ -1351,14 +1379,17 @@ shortcut operator, when used as a general value and not as a Boolean, is the last evaluated argument. It is possible to assign the result of a comparison or other Boolean -expression to a variable, but you must enclose the entire Boolean -expression in parentheses. This is necessary because otherwise an -assignment like \verb/a = b = c/ would be ambiguous: does it assign the -value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to -{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first -meaning is what you get, and to get the latter you have to write -\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur -inside expressions.) +expression to a variable. For example, + +\bcode\begin{verbatim} +>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' +>>> non_null = string1 or string2 or string3 +>>> non_null +'Trondheim' +>>> +\end{verbatim}\ecode +% +Note that in Python, unlike C, assignment cannot occur inside expressions. \section{Comparing Sequences and Other Types} @@ -1368,7 +1399,7 @@ first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, -the lexiographical comparison is carried out recursively. If all +the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial subsequence of the other, the shorted sequence is the smaller one. Lexicographical ordering for @@ -1391,9 +1422,9 @@ Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc. Mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc.% \footnote{ - The rules for comparing objects of different types should - not be relied upon; they may change in a future version of - the language. + The rules for comparing objects of different types should + not be relied upon; they may change in a future version of + the language. } @@ -1418,9 +1449,11 @@ executed at the top level and in calculator mode). A module is a file containing Python definitions and statements. The -file name is the module name with the suffix {\tt .py} appended. For -instance, use your favorite text editor to create a file called {\tt -fibo.py} in the current directory with the following contents: +file name is the module name with the suffix {\tt .py} appended. Within +a module, the module's name (as a string) is available as the value of +the global variable {\tt __name__}. For instance, use your favorite text +editor to create a file called {\tt fibo.py} in the current directory +with the following contents: \bcode\begin{verbatim} # Fibonacci numbers module @@ -1460,6 +1493,8 @@ Using the module name you can access the functions: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] +>>> fibo.__name__ +'fibo' >>> \end{verbatim}\ecode % @@ -1481,9 +1516,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 @@ -1586,9 +1621,11 @@ defines. It returns a sorted list of strings: \bcode\begin{verbatim} >>> import fibo, sys >>> dir(fibo) -['fib', 'fib2'] +['__name__', 'fib', 'fib2'] >>> dir(sys) -['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout'] +['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit', +'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace', +'stderr', 'stdin', 'stdout', 'version'] >>> \end{verbatim}\ecode % @@ -1599,7 +1636,7 @@ Without arguments, {\tt dir()} lists the names you have defined currently: >>> import fibo, sys >>> fib = fibo.fib >>> dir() -['a', 'fib', 'fibo', 'sys'] +['__name__', 'a', 'fib', 'fibo', 'sys'] >>> \end{verbatim}\ecode % @@ -1612,14 +1649,15 @@ If you want a list of those, they are defined in the standard module \bcode\begin{verbatim} >>> import __builtin__ >>> dir(__builtin__) -['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError', 'I -mportError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'MemoryError', ' -NameError', 'None', 'OverflowError', 'RuntimeError', 'SyntaxError', 'SystemE -rror', 'SystemExit', 'TypeError', 'ValueError', 'ZeroDivisionError', 'abs', -'apply', 'chr', 'cmp', 'coerce', 'compile', 'dir', 'divmod', 'eval', 'execfi -le', 'float', 'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'le -n', 'long', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input', - 'reload', 'repr', 'round', 'setattr', 'str', 'type'] +['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError', +'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt', +'MemoryError', 'NameError', 'None', 'OverflowError', 'RuntimeError', +'SyntaxError', 'SystemError', 'SystemExit', 'TypeError', 'ValueError', +'ZeroDivisionError', '__name__', 'abs', 'apply', 'chr', 'cmp', 'coerce', +'compile', 'dir', 'divmod', 'eval', 'execfile', 'filter', 'float', +'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'len', 'long', +'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input', +'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange'] >>> \end{verbatim}\ecode @@ -1638,6 +1676,10 @@ Python is to do all the string handling yourself; using string slicing and concatenation operations you can create any lay-out you can imagine. The standard module {\tt string} contains some useful operations for padding strings to a given column width; these will be discussed shortly. +Finally, the \code{\%} operator (modulo) with a string left argument +interprets this string as a C sprintf format string to be applied to the +right argument, and returns the string resulting from this formatting +operation. One question remains, of course: how do you convert values to strings? Luckily, Python has a way to convert any value to a string: just write @@ -1650,22 +1692,22 @@ the value between reverse quotes (\verb/``/). Some examples: >>> print s The value of x is 31.4, and y is 40000... >>> # Reverse quotes work on other types besides numbers: ->>> p = [x, y] +... p = [x, y] >>> ps = `p` >>> ps '[31.4, 40000]' >>> # Converting a string adds string quotes and backslashes: ->>> hello = 'hello, world\n' +... hello = 'hello, world\n' >>> hellos = `hello` >>> print hellos 'hello, world\012' >>> # The argument of reverse quotes may be a tuple: ->>> `x, y, ('foo', 'bar')` -'(31.4, 40000, (\'foo\', \'bar\'))' +... `x, y, ('foo', 'bar')` +"(31.4, 40000, ('foo', 'bar'))" >>> \end{verbatim}\ecode % -Here is how you write a table of squares and cubes: +Here are two ways to write a table of squares and cubes: \bcode\begin{verbatim} >>> import string @@ -1684,6 +1726,19 @@ Here is how you write a table of squares and cubes: 8 64 512 9 81 729 10 100 1000 +>>> for x in range(1,11): +... print '%2d %3d %4d' % (x, x*x, x*x*x) +... + 1 1 1 + 2 4 8 + 3 9 27 + 4 16 64 + 5 25 125 + 6 36 216 + 7 49 343 + 8 64 512 + 9 81 729 +10 100 1000 >>> \end{verbatim}\ecode % @@ -1702,11 +1757,7 @@ a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.) There is another function, {\tt string.zfill}, which pads a numeric string on the left with zeros. It understands about plus and minus -signs:% -\footnote{ - Better facilities for formatting floating point numbers are - lacking at this moment. -} +signs: \bcode\begin{verbatim} >>> string.zfill('12', 5) @@ -1733,10 +1784,10 @@ kind of complaint you get while you are still learning Python: \bcode\begin{verbatim} >>> while 1 print 'Hello world' -Parsing error: file <stdin>, line 1: -while 1 print 'Hello world' - ^ -Unhandled exception: run-time error: syntax error + File "<stdin>", line 1 + while 1 print 'Hello world' + ^ +SyntaxError: invalid syntax >>> \end{verbatim}\ecode % @@ -1764,11 +1815,11 @@ Traceback (innermost last): File "<stdin>", line 1 ZeroDivisionError: integer division or modulo >>> 4 + foo*3 -Stack backtrace (innermost last): +Traceback (innermost last): File "<stdin>", line 1 NameError: foo >>> '2' + 2 -Stack backtrace (innermost last): +Traceback (innermost last): File "<stdin>", line 1 TypeError: illegal argument type for built-in operation >>> @@ -1910,7 +1961,7 @@ For example: \bcode\begin{verbatim} >>> raise NameError, 'HiThere' -Stack backtrace (innermost last): +Traceback (innermost last): File "<stdin>", line 1 NameError: HiThere >>> @@ -1932,10 +1983,10 @@ For example: ... except my_exc, val: ... print 'My exception occurred, value:', val ... -My exception occured, value: 4 +My exception occurred, value: 4 >>> raise my_exc, 1 -Stack backtrace (innermost last): - File "<stdin>", line 7 +Traceback (innermost last): + File "<stdin>", line 1 my_exc: 1 >>> \end{verbatim}\ecode @@ -1956,7 +2007,7 @@ For example: ... print 'Goodbye, world!' ... Goodbye, world! -Stack backtrace (innermost last): +Traceback (innermost last): File "<stdin>", line 2 KeyboardInterrupt >>> @@ -1964,7 +2015,7 @@ KeyboardInterrupt % A {\tt finally} clause is executed whether or not an exception has occurred in the {\tt try} clause. When an exception has occurred, it -is re-raised after the {\tt finally} clauses is executed. The +is re-raised after the {\tt finally} clause is executed. The {\tt finally} clause is also executed ``on the way out'' when the {\tt try} statement is left via a {\tt break} or {\tt return} statement. @@ -1988,7 +2039,7 @@ same name. Objects can contain an arbitrary amount of private data. In C++ terminology, all class members (including the data members) are {\em public}, and all member functions are {\em virtual}. There are -no special constructors or desctructors. As in Modula-3, there are no +no special constructors or destructors. As in Modula-3, there are no shorthands for referencing the object's members from its methods: the method function is declared with an explicit first argument representing the object, which is provided implicitly by the call. As @@ -1996,9 +2047,9 @@ in Smalltalk, classes themselves are objects, albeit in the wider sense of the word: in Python, all data types are objects. This provides semantics for importing and renaming. But, just like in C++ or Modula-3, built-in types cannot be used as base classes for -extension by the user. Also, like in Modula-3 but unlike in C++, the +extension by the user. Also, like in C++ but unlike in Modula-3, most built-in operators with special syntax (arithmetic operators, -subscriptong etc.) cannot be redefined for class members. +subscripting etc.) can be redefined for class members. \section{A word about terminology} @@ -2022,7 +2073,7 @@ can be bound to the same object. This is known as aliasing in other languages. This is usually not appreciated on a first glance at Python, and can be safely ignored when dealing with immutable basic types (numbers, strings, tuples). However, aliasing has an -(intended!) effect on the semantics of Python code involving mutable +(intended!) effect on the semantics of Python code involving mutable objects such as lists, dictionaries, and most types representing entities outside the program (files, windows, etc.). This is usually used to the benefit of the program, since aliases behave like pointers @@ -2065,13 +2116,13 @@ names in modules are attribute references: in the expression be a straightforward mapping between the module's attributes and the global names defined in the module: they share the same name space!% \footnote{ - Except for one thing. Module objects have a secret read-only - attribute called {\tt __dict__} which returns the dictionary - used to implement the module's name space; the name - {\tt __dict__} is an attribute but not a global name. - Obviously, using this violates the abstraction of name space - implementation, and should be restricted to things like - post-mortem debuggers... + Except for one thing. Module objects have a secret read-only + attribute called {\tt __dict__} which returns the dictionary + used to implement the module's name space; the name + {\tt __dict__} is an attribute but not a global name. + Obviously, using this violates the abstraction of name space + implementation, and should be restricted to things like + post-mortem debuggers... } Attributes may be read-only or writable. In the latter case, @@ -2314,7 +2365,7 @@ avoid accidental name conflicts, which may cause hard-to-find bugs in large programs, it is wise to use some kind of convention that minimizes the chance of conflicts, e.g., capitalize method names, prefix data attribute names with a small unique string (perhaps just -an undescore), or use verbs for methods and nouns for data attributes. +an underscore), or use verbs for methods and nouns for data attributes. Data attributes may be referenced by methods as well as by ordinary @@ -2392,8 +2443,9 @@ Methods may call other methods by using method attributes of the The instantiation operation (``calling'' a class object) creates an empty object. Many classes like to create objects in a known initial -state. There is no special syntax to enforce this, but a convention -works almost as well: add a method named \verb\init\ to the class, +state. In early versions of Python, there was no special syntax to +enforce this (see below), but a convention was widely used: +add a method named \verb\init\ to the class, which initializes the instance (by assigning to some important data attributes) and returns the instance itself. For example, class \verb\Bag\ above could have the following method: @@ -2411,13 +2463,39 @@ statement, as follows: x = Bag().init() \end{verbatim} -Of course, the \verb\init\ method may have arguments for greater -flexibility. +In later versions of Python, a special method named \verb\__init__\ may be +defined instead: + +\begin{verbatim} + def __init__(self): + self.empty() +\end{verbatim} + +When a class defines an \verb\__init__\ method, class instantiation +automatically invokes \verb\__init__\ for the newly-created class +instance. So in the \verb\Bag\ example, a new and initialized instance +can be obtained by: -Warning: a common mistake is to forget the \verb\return self\ at the -end of an init method! +\begin{verbatim} + x = Bag() +\end{verbatim} +Of course, the \verb\__init__\ method may have arguments for greater +flexibility. In that case, arguments given to the class instantiation +operator are passed on to \verb\__init__\. For example, +\bcode\begin{verbatim} +>>> class Complex: +... def __init__(self, realpart, imagpart): +... self.r = realpart +... self.i = imagpart +... +>>> x = Complex(3.0,-4.5) +>>> x.r, x.i +(3.0, -4.5) +>>> +\end{verbatim}\ecode +% Methods may reference global names in the same way as ordinary functions. The global scope associated with a method is the module containing the class definition. (The class itself is never used as a @@ -2484,7 +2562,7 @@ the base class is defined or imported directly in the global scope.) \subsection{Multiple inheritance} -Poython supports a limited form of multiple inheritance as well. A +Python supports a limited form of multiple inheritance as well. A class definition with multiple base classes looks as follows: \begin{verbatim} @@ -2559,7 +2637,422 @@ object of which the method is an instance, and \verb\m.im_func\ is the function object corresponding to the method. -XXX Mention bw compat hacks. +\chapter{Recent Additions} + +Python is an evolving language. Since this tutorial was last +thoroughly revised, several new features have been added to the +language. While ideally I should revise the tutorial to incorporate +them in the mainline of the text, lack of time currently requires me +to a more modest approach. In this chapter I will briefly list the +most important improvements to the language and how you can use them +to your benefit. + +\section{The Last Printed Expression} + +In interactive mode, the last printed expression is assigned to the +variable \code\_\. This means that when you are using Python as a +desk calculator, it is somewhat easier to continue calculations, for +example: + +\begin{verbatim} + >>> tax = 17.5 / 100 + >>> price = 3.50 + >>> price * tax + 0.6125 + >>> price + _ + 4.1125 + >>> round(_, 2) + 4.11 + >>> +\end{verbatim} + +\section{String Literals} + +\subsection{Double Quotes} + +Python can now also use double quotes to surround string literals, +e.g. \verb\"this doesn't hurt a bit"\. + +\subsection{Continuation Of String Literals} + +String literals can span multiple lines by escaping newlines with +backslashes, e.g. + +\begin{verbatim} + hello = "This is a rather long string containing\n\ + several lines of text just as you would do in C.\n\ + Note that whitespace at the beginning of the line is\ + significant.\n" + print hello +\end{verbatim} + +which would print the following: +\begin{verbatim} + This is a rather long string containing + several lines of text just as you would do in C. + Note that whitespace at the beginning of the line is significant. +\end{verbatim} + +\subsection{Triple-quoted strings} + +In some cases, when you need to include really long strings (e.g. +containing several paragraphs of informational text), it is annoying +that you have to terminate each line with \verb@\n\@, especially if +you would like to reformat the text occasionally with a powerful text +editor like Emacs. For such situations, ``triple-quoted'' strings can +be used, e.g. + +\begin{verbatim} + hello = """ + + This string is bounded by triple double quotes (3 times "). + Newlines in the string are retained, though \ + it is still possible\nto use all normal escape sequences. + + Whitespace at the beginning of a line is + significant. If you need to include three opening quotes + you have to escape at least one of them, e.g. \""". + + This string ends in a newline. + """ +\end{verbatim} + +Note that there is no semantic difference between strings quoted with +single quotes (\verb/'/) or double quotes (\verb\"\). + +\subsection{String Literal Juxtaposition} + +One final twist: you can juxtapose multiple string literals. Two or +more adjacent string literals (but not arbitrary expressions!) +separated only by whitespace will be concatenated (without intervening +whitespace) into a single string object at compile time. This makes +it possible to continue a long string on the next line without +sacrificing indentation or performance, unlike the use of the string +concatenation operator \verb\+\ or the continuation of the literal +itself on the next line (since leading whitespace is significant +inside all types of string literals). Note that this feature, like +all string features except triple-quoted strings, is borrowed from +Standard C. + +\section{The Formatting Operator} + +\subsection{Basic Usage} + +The chapter on output formatting is really out of date: there is now +an almost complete interface to C-style printf formats. This is done +by overloading the modulo operator (\verb\%\) for a left operand +which is a string, e.g. + +\begin{verbatim} + >>> import math + >>> print 'The value of PI is approximately %5.3f.' % math.pi + The value of PI is approximately 3.142. + >>> +\end{verbatim} + +If there is more than one format in the string you pass a tuple as +right operand, e.g. + +\begin{verbatim} + >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} + >>> for name, phone in table.items(): + ... print '%-10s ==> %10d' % (name, phone) + ... + Jack ==> 4098 + Dcab ==> 8637678 + Sjoerd ==> 4127 + >>> +\end{verbatim} + +Most formats work exactly as in C and require that you pass the proper +type (however, if you don't you get an exception, not a core dump). +The \verb\%s\ format is more relaxed: if the corresponding argument is +not a string object, it is converted to string using the \verb\str()\ +built-in function. Using \verb\*\ to pass the width or precision in +as a separate (integer) argument is supported. The C formats +\verb\%n\ and \verb\%p\ are not supported. + +\subsection{Referencing Variables By Name} + +If you have a really long format string that you don't want to split +up, it would be nice if you could reference the variables to be +formatted by name instead of by position. This can be done by using +an extension of C formats using the form \verb\%(name)format\, e.g. + +\begin{verbatim} + >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} + >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table + Jack: 4098; Sjoerd: 4127; Dcab: 8637678 + >>> +\end{verbatim} + +This is particularly useful in combination with the new built-in +\verb\vars()\ function, which returns a dictionary containing all +local variables. + +\section{Optional Function Arguments} + +It is now possible to define functions with a variable number of +arguments. There are two forms, which can be combined. + +\subsection{Default Argument Values} + +The most useful form is to specify a default value for one or more +arguments. This creates a function that can be called with fewer +arguments than it is defined, e.g. + +\begin{verbatim} + def ask_ok(prompt, retries = 4, complaint = 'Yes or no, please!'): + while 1: + ok = raw_input(prompt) + if ok in ('y', 'ye', 'yes'): return 1 + if ok in ('n', 'no', 'nop', 'nope'): return 0 + retries = retries - 1 + if retries < 0: raise IOError, 'refusenik user' + print complaint +\end{verbatim} + +This function can be called either like this: +\verb\ask_ok('Do you really want to quit?')\ or like this: +\verb\ask_ok('OK to overwrite the file?', 2)\. + +The default values are evaluated at the point of function definition +in the {\em defining} scope, so that e.g. + +\begin{verbatim} + i = 5 + def f(arg = i): print arg + i = 6 + f() +\end{verbatim} + +will print \verb\5\. + +\subsection{Arbitrary Argument Lists} + +It is also possible to specify that a function can be called with an +arbitrary number of arguments. These arguments will be wrapped up in +a tuple. Before the variable number of arguments, zero or more normal +arguments may occur, e.g. + +\begin{verbatim} + def fprintf(file, format, *args): + file.write(format % args) +\end{verbatim} + +This feature may be combined with the previous, e.g. + +\begin{verbatim} + def but_is_it_useful(required, optional = None, *remains): + print "I don't know" +\end{verbatim} + +\section{Lambda And Functional Programming Tools} + +\subsection{Lambda Forms} + +On popular demand, a few features commonly found in functional +programming languages and Lisp have been added to Python. With the +\verb\lambda\ keyword, small anonymous functions can be created. +Here's a function that returns the sum of its two arguments: +\verb\lambda a, b: a+b\. Lambda forms can be used wherever function +objects are required. They are syntactically restricted to a single +expression. Semantically, they are just syntactic sugar for a normal +function definition. Like nested function definitions, lambda forms +cannot reference variables from the containing scope, but this can be +overcome through the judicious use of default argument values, e.g. + +\begin{verbatim} + def make_incrementor(n): + return lambda(x, incr=n): x+incr +\end{verbatim} + +\subsection{Map, Reduce and Filter} + +Three new built-in functions on sequences are good candidate to pass +lambda forms. + +\subsubsection{Map.} + +\verb\map(function, sequence)\ calls \verb\function(item)\ for each of +the sequence's items and returns a list of the return values. For +example, to compute some cubes: + +\begin{verbatim} + >>> map(lambda x: x*x*x, range(1, 11)) + [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] + >>> +\end{verbatim} + +More than one sequence may be passed; the function must then have as +many arguments as there are sequences and is called with the +corresponding item from each sequence (or \verb\None\ if some sequence +is shorter than another). If \verb\None\ is passed for the function, +a function returning its argument(s) is substituted. + +Combining these two special cases, we see that +\verb\map(None, list1, list2)\ is a convenient way of turning a pair +of lists into a list of pairs. For example: + +\begin{verbatim} + >>> seq = range(8) + >>> map(None, seq, map(lambda x: x*x, seq)) + [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)] + >>> +\end{verbatim} + +\subsubsection{Filter.} + +\verb\filter(function, sequence)\ returns a sequence (of the same +type, if possible) consisting of those items from the sequence for +which \verb\function(item)\ is true. For example, to compute some +primes: + +\begin{verbatim} + >>> filter(lambda x: x%2 != 0 and x%3 != 0, range(2, 25)) + [5, 7, 11, 13, 17, 19, 23] + >>> +\end{verbatim} + +\subsubsection{Reduce.} + +\verb\reduce(function, sequence)\ returns a single value constructed +by calling the (binary) function on the first two items of the +sequence, then on the result and the next item, and so on. For +example, to compute the sum of the numbers 1 through 10: + +\begin{verbatim} + >>> reduce(lambda x, y: x+y, range(1, 11)) + 55 + >>> +\end{verbatim} + +If there's only one item in the sequence, its value is returned; if +the sequence is empty, an exception is raised. + +A third argument can be passed to indicate the starting value. In this +case the starting value is returned for an empty sequence, and the +function is first applied to the starting value and the first sequence +item, then to the result and the next item, and so on. For example, + +\begin{verbatim} + >>> def sum(seq): + ... return reduce(lambda x, y: x+y, seq, 0) + ... + >>> sum(range(1, 11)) + 55 + >>> sum([]) + 0 + >>> +\end{verbatim} + +\section{Continuation Lines Without Backslashes} +While the general mechanism for continuation of a source line on the +next physical line remains to place a backslash on the end of the +line, expressions inside matched parentheses (or square brackets, or +curly braces) can now also be continued without using a backslash. +This is particularly useful for calls to functions with many +arguments, and for initializations of large tables. + +For example: + +\begin{verbatim} + month_names = ['Januari', 'Februari', 'Maart', + 'April', 'Mei', 'Juni', + 'Juli', 'Augustus', 'September', + 'Oktober', 'November', 'December'] +\end{verbatim} + +and + +\begin{verbatim} + CopyInternalHyperLinks(self.context.hyperlinks, + copy.context.hyperlinks, + uidremap) +\end{verbatim} + +\section{Regular Expressions} + +While C's printf-style output formats, transformed into Python, are +adequate for most output formatting jobs, C's scanf-style input +formats are not very powerful. Instead of scanf-style input, Python +offers Emacs-style regular expressions as a powerful input and +scanning mechanism. Read the corresponding section in the Library +Reference for a full description. + +\section{Generalized Dictionaries} + +The keys of dictionaries are no longer restricted to strings -- they +can be numbers, tuples, or (certain) class instances. (Lists and +dictionaries are not acceptable as dictionary keys, in order to avoid +problems when the object used as a key is modified.) + +Dictionaries have two new methods: \verb\d.values()\ returns a list of +the dictionary's values, and \verb\d.items()\ returns a list of the +dictionary's (key, value) pairs. Like \verb\d.keys()\, these +operations are slow for large dictionaries. Examples: + +\begin{verbatim} + >>> d = {100: 'honderd', 1000: 'duizend', 10: 'tien'} + >>> d.keys() + [100, 10, 1000] + >>> d.values() + ['honderd', 'tien', 'duizend'] + >>> d.items() + [(100, 'honderd'), (10, 'tien'), (1000, 'duizend')] + >>> +\end{verbatim} + +\section{Miscellaneous New Built-in Functions} + +The function \verb\vars()\ returns a dictionary containing the current +local variables. With a module as argument, it returns that module's +global variables. The old function \verb\dir(x)\ returns +\verb\vars(x).keys()\. + +The function \verb\round(x)\ returns a floating point number rounded +to the nearest integer (but still expressed as a floating point +number). E.g. \verb\round(3.4) == 3.0\ and \verb\round(3.5) == 4.0\. +With a second argument it rounds to the specified number of digits, +e.g. \verb\round(math.pi, 4) == 3.1416\ or even +\verb\round(123.4, -2) == 100.0\. + +The function \verb\hash(x)\ returns a hash value for an object. +All object types acceptable as dictionary keys have a hash value (and +it is this hash value that the dictionary implementation uses). + +The function \verb\id(x)\ return a unique identifier for an object. +For two objects x and y, \verb\id(x) == id(y)\ if and only if +\verb\x is y\. (In fact the object's address is used.) + +The function \verb\hasattr(x, name)\ returns whether an object has an +attribute with the given name (a string value). The function +\verb\getattr(x, name)\ returns the object's attribute with the given +name. The function \verb\setattr(x, name, value)\ assigns a value to +an object's attribute with the given name. These three functions are +useful if the attribute names are not known beforehand. Note that +\verb\getattr(x, 'foo')\ is equivalent to \verb\x.foo\, and +\verb\setattr(x, 'foo', y)\ is equivalent to \verb\x.foo = y\. By +definition, \verb\hasattr(x, name)\ returns true if and only if +\verb\getattr(x, name)\ returns without raising an exception. + +\section{Else Clause For Try Statement} + +The \verb\try...except\ statement now has an optional \verb\else\ +clause, which must follow all \verb\except\ clauses. It is useful to +place code that must be executed if the \verb\try\ clause does not +raise an exception. For example: + +\begin{verbatim} + for arg in sys.argv: + try: + f = open(arg, 'r') + except IOError: + print 'cannot open', arg + else: + print arg, 'has', len(f.readlines()), 'lines' + f.close() +\end{verbatim} \end{document} |