diff options
Diffstat (limited to 'Doc/tutorial')
-rw-r--r-- | Doc/tutorial/appendix.rst | 2 | ||||
-rw-r--r-- | Doc/tutorial/classes.rst | 9 | ||||
-rw-r--r-- | Doc/tutorial/controlflow.rst | 2 | ||||
-rw-r--r-- | Doc/tutorial/errors.rst | 14 | ||||
-rw-r--r-- | Doc/tutorial/inputoutput.rst | 4 | ||||
-rw-r--r-- | Doc/tutorial/introduction.rst | 8 | ||||
-rw-r--r-- | Doc/tutorial/modules.rst | 2 | ||||
-rw-r--r-- | Doc/tutorial/stdlib.rst | 2 | ||||
-rw-r--r-- | Doc/tutorial/stdlib2.rst | 1 |
9 files changed, 26 insertions, 18 deletions
diff --git a/Doc/tutorial/appendix.rst b/Doc/tutorial/appendix.rst index 7ab588b..73b7d1b 100644 --- a/Doc/tutorial/appendix.rst +++ b/Doc/tutorial/appendix.rst @@ -92,7 +92,7 @@ in the script:: filename = os.environ.get('PYTHONSTARTUP') if filename and os.path.isfile(filename): with open(filename) as fobj: - startup_file = fobj.read() + startup_file = fobj.read() exec(startup_file) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index d82adf6..b1f3f00 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -210,6 +210,7 @@ definition looked like this:: class MyClass: """A simple example class""" i = 12345 + def f(self): return 'hello world' @@ -458,8 +459,10 @@ variable in the class is also ok. For example:: class C: f = f1 + def g(self): return 'hello world' + h = g Now ``f``, ``g`` and ``h`` are all attributes of class :class:`C` that refer to @@ -473,8 +476,10 @@ argument:: 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) @@ -670,7 +675,7 @@ will do nicely:: class Employee: pass - john = Employee() # Create an empty employee record + john = Employee() # Create an empty employee record # Fill the fields of the record john.name = 'John Doe' @@ -796,8 +801,10 @@ defines :meth:`~iterator.next`, then :meth:`__iter__` can just return ``self``:: 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 diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst index 8ffaf3f..2af60d0 100644 --- a/Doc/tutorial/controlflow.rst +++ b/Doc/tutorial/controlflow.rst @@ -284,7 +284,7 @@ You can see it if you really want to using :keyword:`print`:: It is simple to write a function that returns a list of the numbers of the Fibonacci series, instead of printing it:: - >>> def fib2(n): # return Fibonacci series up to n + >>> def fib2(n): # return Fibonacci series up to n ... """Return a list containing the Fibonacci series up to n.""" ... result = [] ... a, b = 0, 1 diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst index 6f77def..f93a544 100644 --- a/Doc/tutorial/errors.rst +++ b/Doc/tutorial/errors.rst @@ -180,14 +180,14 @@ One may also instantiate an exception first before raising it and add any attributes to it as desired. :: >>> try: - ... raise Exception('spam', 'eggs') + ... raise Exception('spam', 'eggs') ... except Exception as inst: - ... print type(inst) # the exception instance - ... print inst.args # arguments stored in .args - ... print inst # __str__ allows args to be printed directly - ... x, y = inst.args - ... print 'x =', x - ... print 'y =', y + ... print type(inst) # the exception instance + ... print inst.args # arguments stored in .args + ... print inst # __str__ allows args to be printed directly + ... x, y = inst.args + ... print 'x =', x + ... print 'y =', y ... <type 'exceptions.Exception'> ('spam', 'eggs') diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index 6fdc5f0..3e32af2 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -326,10 +326,10 @@ beginning of the file as the reference point. :: >>> f = open('workfile', 'r+') >>> f.write('0123456789abcdef') - >>> f.seek(5) # Go to the 6th byte in the file + >>> f.seek(5) # Go to the 6th byte in the file >>> f.read(1) '5' - >>> f.seek(-3, 2) # Go to the 3rd byte before the end + >>> f.seek(-3, 2) # Go to the 3rd byte before the end >>> f.read(1) 'd' diff --git a/Doc/tutorial/introduction.rst b/Doc/tutorial/introduction.rst index a4fb70c..b7be00e 100644 --- a/Doc/tutorial/introduction.rst +++ b/Doc/tutorial/introduction.rst @@ -236,7 +236,7 @@ If you want to concatenate variables or a variable and a literal, use ``+``:: This feature is particularly useful when you want to break long strings:: >>> text = ('Put several strings within parentheses ' - 'to have them joined together.') + ... 'to have them joined together.') >>> text 'Put several strings within parentheses to have them joined together.' @@ -280,11 +280,11 @@ makes sure that ``s[:i] + s[i:]`` is always equal to ``s``:: Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced. :: - >>> word[:2] # character from the beginning to position 2 (excluded) + >>> word[:2] # character from the beginning to position 2 (excluded) 'Py' - >>> word[4:] # characters from position 4 (included) to the end + >>> word[4:] # characters from position 4 (included) to the end 'on' - >>> word[-2:] # characters from the second-last (included) to the end + >>> word[-2:] # characters from the second-last (included) to the end 'on' One way to remember how slices work is to think of the indices as pointing diff --git a/Doc/tutorial/modules.rst b/Doc/tutorial/modules.rst index f5b8114..7b6fd9c 100644 --- a/Doc/tutorial/modules.rst +++ b/Doc/tutorial/modules.rst @@ -33,7 +33,7 @@ called :file:`fibo.py` in the current directory with the following contents:: print b, a, b = b, a+b - def fib2(n): # return Fibonacci series up to n + def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst index f6239d6..4dab074 100644 --- a/Doc/tutorial/stdlib.rst +++ b/Doc/tutorial/stdlib.rst @@ -283,7 +283,7 @@ file:: with self.assertRaises(TypeError): average(20, 30, 70) - unittest.main() # Calling from the command line invokes all tests + unittest.main() # Calling from the command line invokes all tests .. _tut-batteries-included: diff --git a/Doc/tutorial/stdlib2.rst b/Doc/tutorial/stdlib2.rst index 65fb093..50b74d1 100644 --- a/Doc/tutorial/stdlib2.rst +++ b/Doc/tutorial/stdlib2.rst @@ -178,6 +178,7 @@ tasks in background while the main program continues to run:: 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) |