diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-05-10 09:01:23 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-05-10 09:01:23 (GMT) |
commit | dba903993a8d3e13d2cf83d6a8912e908025b17b (patch) | |
tree | b0f7d957452d40ce384e5d0a1382067e3379f60f /Doc/faq | |
parent | 387235085c5a6a1d823b0af3fabb42830c88f984 (diff) | |
download | cpython-dba903993a8d3e13d2cf83d6a8912e908025b17b.zip cpython-dba903993a8d3e13d2cf83d6a8912e908025b17b.tar.gz cpython-dba903993a8d3e13d2cf83d6a8912e908025b17b.tar.bz2 |
Issue #23921: Standardized documentation whitespace formatting.
Original patch by James Edwards.
Diffstat (limited to 'Doc/faq')
-rw-r--r-- | Doc/faq/design.rst | 18 | ||||
-rw-r--r-- | Doc/faq/library.rst | 17 | ||||
-rw-r--r-- | Doc/faq/programming.rst | 39 |
3 files changed, 39 insertions, 35 deletions
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index c42cccb..1b6cd7e 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -158,7 +158,7 @@ where in Python you're forced to write this:: line = f.readline() if not line: break - ... # do something with line + ... # do something with line The reason for not allowing assignment in Python expressions is a common, hard-to-find bug in those other languages, caused by this construct: @@ -190,7 +190,7 @@ generally less robust than the "while True" solution:: line = f.readline() while line: - ... # do something with line... + ... # do something with line... line = f.readline() The problem with this is that if you change your mind about exactly how you get @@ -203,7 +203,7 @@ objects using the ``for`` statement. For example, :term:`file objects <file object>` support the iterator protocol, so you can write simply:: for line in f: - ... # do something with line... + ... # do something with line... @@ -577,8 +577,10 @@ other structure). :: class ListWrapper: def __init__(self, the_list): self.the_list = the_list + def __eq__(self, other): return self.the_list == other.the_list + def __hash__(self): l = self.the_list result = 98767 - len(l)*555 @@ -619,7 +621,7 @@ it and returns it. For example, here's how to iterate over the keys of a dictionary in sorted order:: for key in sorted(mydict): - ... # do whatever with mydict[key]... + ... # do whatever with mydict[key]... How do you specify and enforce an interface spec in Python? @@ -675,11 +677,11 @@ languages. For example:: class label(Exception): pass # declare a label try: - ... - if condition: raise label() # goto label - ... + ... + if condition: raise label() # goto label + ... except label: # where to goto - pass + pass ... This doesn't allow you to jump into the middle of a loop, but that's usually diff --git a/Doc/faq/library.rst b/Doc/faq/library.rst index 2f82a0c..b5fdfa4 100644 --- a/Doc/faq/library.rst +++ b/Doc/faq/library.rst @@ -257,7 +257,8 @@ all the threads to finish:: import threading, time def thread_task(name, n): - for i in range(n): print(name, i) + for i in range(n): + print(name, i) for i in range(10): T = threading.Thread(target=thread_task, args=(str(i), i)) @@ -273,7 +274,8 @@ A simple fix is to add a tiny sleep to the start of the run function:: def thread_task(name, n): time.sleep(0.001) # <--------------------! - for i in range(n): print(name, i) + for i in range(n): + print(name, i) for i in range(10): T = threading.Thread(target=thread_task, args=(str(i), i)) @@ -502,8 +504,8 @@ in big-endian format from a file:: import struct with open(filename, "rb") as f: - s = f.read(8) - x, y, z = struct.unpack(">hhl", s) + s = f.read(8) + x, y, z = struct.unpack(">hhl", s) The '>' in the format string forces big-endian data; the letter 'h' reads one "short integer" (2 bytes), and 'l' reads one "long integer" (4 bytes) from the @@ -681,10 +683,10 @@ Yes. Here's a simple example that uses urllib.request:: import urllib.request - ### build the query string + # build the query string qs = "First=Josephine&MI=Q&Last=Public" - ### connect and send the server a path + # connect and send the server a path req = urllib.request.urlopen('http://www.some-server.out-there' '/cgi-bin/some-cgi-script', data=qs) with req: @@ -740,8 +742,9 @@ varies between systems; sometimes it is ``/usr/lib/sendmail``, sometimes ``/usr/sbin/sendmail``. The sendmail manual page will help you out. Here's some sample code:: - SENDMAIL = "/usr/sbin/sendmail" # sendmail location import os + + SENDMAIL = "/usr/sbin/sendmail" # sendmail location p = os.popen("%s -t -i" % SENDMAIL, "w") p.write("To: receiver@example.com\n") p.write("Subject: test\n") diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 567614c..461a65b 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -207,7 +207,7 @@ functions), e.g.:: >>> squares = [] >>> for x in range(5): - ... squares.append(lambda: x**2) + ... squares.append(lambda: x**2) This gives you a list that contains 5 lambdas that calculate ``x**2``. You might expect that, when called, they would return, respectively, ``0``, ``1``, @@ -234,7 +234,7 @@ lambdas, so that they don't rely on the value of the global ``x``:: >>> squares = [] >>> for x in range(5): - ... squares.append(lambda n=x: n**2) + ... squares.append(lambda n=x: n**2) Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed when the lambda is defined so that it has the same value that ``x`` had at @@ -539,7 +539,7 @@ desired effect in a number of ways. args['a'] = 'new-value' # args is a mutable dictionary args['b'] = args['b'] + 1 # change it in-place - args = {'a':' old-value', 'b': 99} + args = {'a': 'old-value', 'b': 99} func3(args) print(args['a'], args['b']) @@ -655,16 +655,15 @@ Essentially, assignment always binds a name to a value; The same is true of ``def`` and ``class`` statements, but in that case the value is a callable. Consider the following code:: - class A: - pass - - B = A - - a = B() - b = a - print(b) + >>> class A: + ... pass + ... + >>> B = A + >>> a = B() + >>> b = a + >>> print(b) <__main__.A object at 0x16D07CC> - print(a) + >>> print(a) <__main__.A object at 0x16D07CC> Arguably the class has a name: even though it is bound to two names and invoked @@ -1099,7 +1098,7 @@ How do I iterate over a sequence in reverse order? Use the :func:`reversed` built-in function, which is new in Python 2.4:: for x in reversed(sequence): - ... # do something with x... + ... # do something with x ... This won't touch your original sequence, but build a new copy with reversed order to iterate over. @@ -1107,7 +1106,7 @@ order to iterate over. With Python 2.3, you can use an extended slice syntax:: for x in sequence[::-1]: - ... # do something with x... + ... # do something with x ... How do you remove duplicates from a list? @@ -1405,7 +1404,7 @@ A method is a function on some object ``x`` that you normally call as definition:: class C: - def meth (self, arg): + def meth(self, arg): return arg * 2 + self.attribute @@ -1438,9 +1437,9 @@ that does something:: def search(obj): if isinstance(obj, Mailbox): - # ... code to search a mailbox + ... # code to search a mailbox elif isinstance(obj, Document): - # ... code to search a document + ... # code to search a document elif ... A better approach is to define a ``search()`` method on all the classes and just @@ -1448,11 +1447,11 @@ call it:: class Mailbox: def search(self): - # ... code to search a mailbox + ... # code to search a mailbox class Document: def search(self): - # ... code to search a document + ... # code to search a document obj.search() @@ -1509,7 +1508,7 @@ How do I call a method defined in a base class from a derived class that overrid Use the built-in :func:`super` function:: class Derived(Base): - def meth (self): + def meth(self): super(Derived, self).meth() For version prior to 3.0, you may be using classic classes: For a class |