diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/classes.rst --- a/Doc/tutorial/classes.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/classes.rst Mon Mar 07 14:27:44 2011 +0900 @@ -209,6 +209,7 @@ class MyClass: """A simple example class""" i = 12345 + def f(self): return 'hello world' @@ -401,8 +402,10 @@ class Bag: def __init__(self): self.data = [] + def add(self, x): self.data.append(x) + def addtwice(self, x): self.add(x) self.add(x) @@ -625,14 +628,16 @@ class B: pass + class C(B): pass + class D(C): pass - for c in [B, C, D]: + for cls in [B, C, D]: try: - raise c() + raise cls() except D: print "D" except C: @@ -660,7 +665,7 @@ print element for element in (1, 2, 3): print element - for key in {'one':1, 'two':2}: + for key in {'one': 1, 'two': 2}: print key for char in "123": print char @@ -702,12 +707,14 @@ def __init__(self, data): self.data = data self.index = len(data) + def __iter__(self): return self + def next(self): if self.index == 0: - raise StopIteration - self.index = self.index - 1 + raise StopIteration() + self.index -= 1 return self.data[self.index] >>> rev = Reverse('spam') @@ -776,23 +783,23 @@ Examples:: - >>> sum(i*i for i in range(10)) # sum of squares + >>> sum(i * i for i in range(10)) # sum of squares 285 >>> xvec = [10, 20, 30] >>> yvec = [7, 5, 3] - >>> sum(x*y for x,y in zip(xvec, yvec)) # dot product + >>> sum(x * y for x, y in zip(xvec, yvec)) # dot product 260 >>> from math import pi, sin - >>> sine_table = dict((x, sin(x*pi/180)) for x in range(0, 91)) + >>> sine_table = dict((x, sin(x * pi / 180)) for x in range(0, 91)) >>> unique_words = set(word for line in page for word in line.split()) >>> valedictorian = max((student.gpa, student.name) for student in graduates) >>> data = 'golf' - >>> list(data[i] for i in range(len(data)-1,-1,-1)) + >>> list(data[i] for i in range(len(data) - 1, -1, -1)) ['f', 'l', 'o', 'g'] diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/controlflow.rst --- a/Doc/tutorial/controlflow.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/controlflow.rst Mon Mar 07 14:27:44 2011 +0900 @@ -44,7 +44,6 @@ .. index:: statement: for - statement: for The :keyword:`for` statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression @@ -196,7 +195,7 @@ ... a, b = 0, 1 ... while a < n: ... print a, - ... a, b = b, a+b + ... a, b = b, a + b ... >>> # Now call the function we just defined: ... fib(2000) @@ -266,7 +265,7 @@ ... a, b = 0, 1 ... while a < n: ... result.append(a) # see below - ... a, b = b, a+b + ... a, b = b, a + b ... return result ... >>> f100 = fib2(100) # call it diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/datastructures.rst --- a/Doc/tutorial/datastructures.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/datastructures.rst Mon Mar 07 14:27:44 2011 +0900 @@ -172,7 +172,8 @@ :class:`string` or :class:`tuple`, the result will be of the same type; otherwise, it is always a :class:`list`. For example, to compute some primes:: - >>> def f(x): return x % 2 != 0 and x % 3 != 0 + >>> def f(x): + ... return x % 2 != 0 and x % 3 != 0 ... >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23] @@ -181,7 +182,8 @@ items and returns a list of the return values. For example, to compute some cubes:: - >>> def cube(x): return x*x*x + >>> def cube(x): + ... return x ** 3 ... >>> map(cube, range(1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] @@ -192,7 +194,8 @@ example:: >>> seq = range(8) - >>> def add(x, y): return x+y + >>> def add(x, y): + ... return x + y ... >>> map(add, seq, seq) [0, 2, 4, 6, 8, 10, 12, 14] @@ -202,7 +205,8 @@ result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10:: - >>> def add(x,y): return x+y + >>> def add(x, y): + ... return x + y ... >>> reduce(add, range(1, 11)) 55 @@ -216,7 +220,8 @@ and the next item, and so on. For example, :: >>> def sum(seq): - ... def add(x,y): return x+y + ... def add(x,y): + ... return x+y ... return reduce(add, seq, 0) ... >>> sum(range(1, 11)) @@ -247,34 +252,34 @@ >>> [weapon.strip() for weapon in freshfruit] ['banana', 'loganberry', 'passion fruit'] >>> vec = [2, 4, 6] - >>> [3*x for x in vec] + >>> [3 * x for x in vec] [6, 12, 18] - >>> [3*x for x in vec if x > 3] + >>> [3 * x for x in vec if x > 3] [12, 18] - >>> [3*x for x in vec if x < 2] + >>> [3 * x for x in vec if x < 2] [] - >>> [[x,x**2] for x in vec] + >>> [[x, x ** 2] for x in vec] [[2, 4], [4, 16], [6, 36]] - >>> [x, x**2 for x in vec] # error - parens required for tuples + >>> [x, x ** 2 for x in vec] # error - parens required for tuples File "", line 1, in ? - [x, x**2 for x in vec] - ^ + [x, x ** 2 for x in vec] + ^ SyntaxError: invalid syntax - >>> [(x, x**2) for x in vec] + >>> [(x, x ** 2) for x in vec] [(2, 4), (4, 16), (6, 36)] >>> vec1 = [2, 4, 6] >>> vec2 = [4, 3, -9] - >>> [x*y for x in vec1 for y in vec2] + >>> [x * y for x in vec1 for y in vec2] [8, 6, -18, 16, 12, -36, 24, 18, -54] - >>> [x+y for x in vec1 for y in vec2] + >>> [x + y for x in vec1 for y in vec2] [6, 5, -7, 8, 7, -5, 10, 9, -3] - >>> [vec1[i]*vec2[i] for i in range(len(vec1))] + >>> [vec1[i] * vec2[i] for i in range(len(vec1))] [8, 12, -54] List comprehensions are much more flexible than :func:`map` and can be applied to complex expressions and nested functions:: - >>> [str(round(355/113.0, i)) for i in range(1,6)] + >>> [str(round(355 / 113.0, i)) for i in range(1, 6)] ['3.1', '3.14', '3.142', '3.1416', '3.14159'] @@ -507,7 +512,7 @@ >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) {'sape': 4139, 'jack': 4098, 'guido': 4127} - >>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension + >>> dict([(x, x ** 2) for x in (2, 4, 6)]) # use a list comprehension {2: 4, 4: 16, 6: 36} Later in the tutorial, we will learn about Generator Expressions which are even @@ -561,7 +566,7 @@ To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the :func:`reversed` function. :: - >>> for i in reversed(xrange(1,10,2)): + >>> for i in reversed(xrange(1, 10, 2)): ... print i ... 9 diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/errors.rst --- a/Doc/tutorial/errors.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/errors.rst Mon Mar 07 14:27:44 2011 +0900 @@ -42,11 +42,11 @@ how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here:: - >>> 10 * (1/0) + >>> 10 * (1 / 0) Traceback (most recent call last): File "", line 1, in ? ZeroDivisionError: integer division or modulo by zero - >>> 4 + spam*3 + >>> 4 + spam * 3 Traceback (most recent call last): File "", line 1, in ? NameError: name 'spam' is not defined @@ -195,7 +195,7 @@ indirectly) in the try clause. For example:: >>> def this_fails(): - ... x = 1/0 + ... x = 1 / 0 ... >>> try: ... this_fails() @@ -327,7 +327,7 @@ example:: >>> try: - ... raise KeyboardInterrupt + ... raise KeyboardInterrupt() ... finally: ... print 'Goodbye, world!' ... diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/floatingpoint.rst --- a/Doc/tutorial/floatingpoint.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/floatingpoint.rst Mon Mar 07 14:27:44 2011 +0900 @@ -160,37 +160,37 @@ convert 0.1 to the closest fraction it can of the form *J*/2**\ *N* where *J* is an integer containing exactly 53 bits. Rewriting :: - 1 / 10 ~= J / (2**N) + 1 / 10 ~= J / (2 ** N) as :: - J ~= 2**N / 10 + J ~= 2 ** N / 10 -and recalling that *J* has exactly 53 bits (is ``>= 2**52`` but ``< 2**53``), -the best value for *N* is 56:: +and recalling that *J* has exactly 53 bits (is ``>= 2 ** 52`` but +``< 2 ** 53``), the best value for *N* is 56:: - >>> 2**52 + >>> 2 ** 52 4503599627370496L - >>> 2**53 + >>> 2 ** 53 9007199254740992L - >>> 2**56/10 + >>> 2 ** 56 / 10 7205759403792793L That is, 56 is the only value for *N* that leaves *J* with exactly 53 bits. The best possible value for *J* is then that quotient rounded:: - >>> q, r = divmod(2**56, 10) + >>> q, r = divmod(2 ** 56, 10) >>> r 6L Since the remainder is more than half of 10, the best approximation is obtained by rounding up:: - >>> q+1 + >>> q + 1 7205759403792794L Therefore the best possible approximation to 1/10 in 754 double precision is -that over 2\*\*56, or :: +that over 2 \*\* 56, or :: 7205759403792794 / 72057594037927936 @@ -201,13 +201,13 @@ So the computer never "sees" 1/10: what it sees is the exact fraction given above, the best 754 double approximation it can get:: - >>> .1 * 2**56 + >>> .1 * 2 ** 56 7205759403792794.0 If we multiply that fraction by 10\*\*30, we can see the (truncated) value of its 30 most significant decimal digits:: - >>> 7205759403792794 * 10**30 / 2**56 + >>> 7205759403792794 * 10 ** 30 / 2 ** 56 100000000000000005551115123125L meaning that the exact number stored in the computer is approximately equal to diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/inputoutput.rst --- a/Doc/tutorial/inputoutput.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/inputoutput.rst Mon Mar 07 14:27:44 2011 +0900 @@ -70,9 +70,9 @@ Here are two ways to write a table of squares and cubes:: >>> for x in range(1, 11): - ... print repr(x).rjust(2), repr(x*x).rjust(3), + ... print repr(x).rjust(2), repr(x * x).rjust(3), ... # Note trailing comma on previous line - ... print repr(x*x*x).rjust(4) + ... print repr(x ** 3).rjust(4) ... 1 1 1 2 4 8 @@ -86,7 +86,7 @@ 10 100 1000 >>> for x in range(1,11): - ... print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x) + ... print '{0:2d} {1:3d} {2:4d}'.format(x, x * x, x ** 3) ... 1 1 1 2 4 8 diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/introduction.rst --- a/Doc/tutorial/introduction.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/introduction.rst Mon Mar 07 14:27:44 2011 +0900 @@ -46,26 +46,26 @@ operators ``+``, ``-``, ``*`` and ``/`` work just like in most other languages (for example, Pascal or C); parentheses can be used for grouping. For example:: - >>> 2+2 + >>> 2 + 2 4 >>> # This is a comment - ... 2+2 + ... 2 + 2 4 - >>> 2+2 # and a comment on the same line as code + >>> 2 + 2 # and a comment on the same line as code 4 - >>> (50-5*6)/4 + >>> (50 - 5 * 6) / 4 5 >>> # Integer division returns the floor: - ... 7/3 + ... 7 / 3 2 - >>> 7/-3 + >>> 7 / -3 -3 The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:: >>> width = 20 - >>> height = 5*9 + >>> height = 5 * 9 >>> width * height 900 @@ -105,18 +105,18 @@ (-1+0j) >>> 1j * complex(0,1) (-1+0j) - >>> 3+1j*3 + >>> 3 + 1j * 3 (3+3j) - >>> (3+1j)*3 + >>> (3 + 1j) * 3 (9+3j) - >>> (1+2j)/(1+1j) + >>> (1 + 2j) / (1 + 1j) (1.5+0.5j) Complex numbers are always represented as two floating point numbers, the real and imaginary part. To extract these parts from a complex number *z*, use ``z.real`` and ``z.imag``. :: - >>> a=1.5+0.5j + >>> a = 1.5 + 0.5j >>> a.real 1.5 >>> a.imag @@ -127,7 +127,7 @@ correct way to convert a complex number to a real number. Use ``abs(z)`` to get its magnitude (as a float) or ``z.real`` to get its real part. :: - >>> a=3.0+4.0j + >>> a = 3.0 + 4.0j >>> float(a) Traceback (most recent call last): File "", line 1, in ? @@ -136,7 +136,7 @@ 3.0 >>> a.imag 4.0 - >>> abs(a) # sqrt(a.real**2 + a.imag**2) + >>> abs(a) # sqrt(a.real ** 2 + a.imag ** 2) 5.0 In interactive mode, the last printed expression is assigned to the variable @@ -603,7 +603,7 @@ ... a, b = 0, 1 >>> while b < 10: ... print b - ... a, b = b, a+b + ... a, b = b, a + b ... 1 1 @@ -644,7 +644,7 @@ and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely, like this:: - >>> i = 256*256 + >>> i = 256 * 256 >>> print 'The value of i is', i The value of i is 65536 @@ -653,7 +653,7 @@ >>> a, b = 0, 1 >>> while b < 1000: ... print b, - ... a, b = b, a+b + ... a, b = b, a + b ... 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/modules.rst --- a/Doc/tutorial/modules.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/modules.rst Mon Mar 07 14:27:44 2011 +0900 @@ -38,7 +38,7 @@ a, b = 0, 1 while b < n: result.append(b) - a, b = b, a+b + a, b = b, a + b return result Now enter the Python interpreter and import this module with the following diff -r 50166a4bcfc6 -r a3a0b31e2dc0 Doc/tutorial/stdlib2.rst --- a/Doc/tutorial/stdlib2.rst Sat Mar 05 20:40:50 2011 +0100 +++ b/Doc/tutorial/stdlib2.rst Mon Mar 07 14:27:44 2011 +0900 @@ -177,6 +177,7 @@ threading.Thread.__init__(self) self.infile = infile self.outfile = outfile + def run(self): f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) f.write(self.infile)