官术网_书友最值得收藏!

Conventions Used in This Book

Code examples in this book are shown in a lucida console text:

>>> def square(x):
... return x * x
...

Some of our examples show code saved in files, and others — such as the one above — are from interactive Python sessions. In such interactive cases, we include the prompts from the Python session such as the triple-arrow >>> and triple-dot ... prompts. You don't need to type these arrows or dots. Similarly, for operating system shell-commands we will use a dollar prompt $ for Linux, macOS and other Unixes, or where the particular operating system is unimportant for the task at hand:

$ python3 words.py

In this case, you don't need to type the $ character.

For Windows-specific commands we will use a leading greater-than prompt:

> python words.py

Again, there's no need to type the > character. For code blocks which need to be placed in a file, rather than entered interactively, we show code without any leading prompts:

def write_sequence(filename, num):
"""Write Recaman's sequence to a text file."""
with open(filename, mode='wt', encoding='utf-8') as f:
f.writelines("{0}\n".format(r)
for r in islice(sequence(), num + 1))

We've worked hard to make sure that our lines of code are short enough so that each single logical line of code corresponds to a single physical line of code in your book. However, the vagaries of publishing e-books to different devices and the very genuine need for occasional long lines of code mean we can't guarantee that lines don't wrap. What we can guarantee, however, is that where a line does wrap, the publisher has inserted a backslash character \ in the final column. You need to use your judgement to determine whether this character is legitimate part of the code or has been added by the e-book platform.

>>> print("This is a single line of code which is very long. Too long, in fact, to fit on a single physical line of code in the book.")

If you see a backslash at the end of the line within the above quoted string, it is not part of the code, and should not be entered.

Occasionally, we'll number lines of code so we can refer to them easily from the narrative next. These line numbers should not be entered as part of the code. Numbered code blocks look like this:

def write_grayscale(filename, pixels):
height = len(pixels)
width = len(pixels[0])

with open(filename, 'wb') as bmp:
# BMP Header
bmp.write(b'BM')

# The next four bytes hold the filesize as a 32-bit
# little-endian integer. Zero placeholder for now.
size_bookmark = bmp.tell()
bmp.write(b'\x00\x00\x00\x00')

Sometimes we need to present code snippets which are incomplete. Usually this is for brevity where we are adding code to an existing block, and where we want to be clear about the block structure without repeating all existing contents of the block. In such cases we use a Python comment containing three dots # ... to indicate the elided code:

class Flight:

# ...

def make_boarding_cards(self, card_printer):
for passenger, seat in sorted(self._passenger_seats()):
card_printer(passenger, seat, self.number(),
self.aircraft_model())

Here it is implied that some other code already exists within the Flight class block before the make_boarding_cards() function.

Finally, within the text of the book, when we are referring to an identifier which is also a function we will use the identifier with empty parentheses, just as we did with make_boarding_cards() in the preceding paragraph.

主站蜘蛛池模板: 南安市| 泌阳县| 兖州市| 土默特左旗| 镇原县| 冷水江市| 灵台县| 美姑县| 巴林左旗| 屯昌县| 福安市| 阳新县| 紫金县| 湟中县| 汉中市| 抚松县| 平遥县| 怀宁县| 雷波县| 申扎县| 宣武区| 仪征市| 南宁市| 和田市| 泗阳县| 许昌县| 志丹县| 双柏县| 乐亭县| 元氏县| 曲沃县| 黑水县| 宁阳县| 申扎县| 瑞金市| 津南区| 甘谷县| 民乐县| 赤峰市| 台前县| 大荔县|