diff --git a/humansize.py b/humansize.py index cd92778..2f40c82 100644 --- a/humansize.py +++ b/humansize.py @@ -12,8 +12,8 @@ Examples: """ -SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'), - 1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')} +SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], + 1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']} def approximate_size(size, a_kilobyte_is_1024_bytes=True): """Convert a file size to human-readable form. diff --git a/native-datatypes.html b/native-datatypes.html index 0edb74f..7d7582a 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -195,24 +195,28 @@ body{counter-reset:h1 2}
a_list[:] is shorthand for making a complete copy of a list.
Lists are implemented as classes. “Creating” a list is really instantiating a class. As such, a list has methods that operate on it. -
>>> a_list -['a', 'b', 'mpilgrim', 'z', 'example'] ->>> a_list.append('new') ① +There are four ways to add items to a list. +
+>>> a_list = ['a'] +>>> a_list = a_list + [2.0, 3] ① >>> a_list -['a', 'b', 'mpilgrim', 'z', 'example', 'new'] ->>> a_list.insert(2, 'new') ② +['a', 2.0, 3] +>>> a_list.append(True) ② >>> a_list -['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new'] ->>> a_list.extend(['two', 'items']) ③ +['a', 2.0, 3, True] +>>> a_list.extend(['four', 'e']) ③ >>> a_list -['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'items']+['a', 2.0, 3, True, 'four', 'e'] +>>> a_list.insert(1, 'a') ④ +>>> a_list +['a', 'a', 2.0, 3, True, 'four', 'e']
append() method adds a single item to the end of the list.
-insert() method inserts a single item into a list. The numeric argument is the index of the first item that gets bumped out of position. List items do not need to be unique; for example, there are now two separate items with the value 'new', a_list[2] and a_list[6].
-extend() method concatenates lists. You do not call extend() with multiple arguments; you call it with one argument, a list. In this case, that list has two items.
++ operator concatenates lists. A list can contain any number of items; there is no size limit (other than available memory). A list can contain items of any datatype; they don't all need to be the same type. Here we have a list containing a string, a floating point number, and an integer.
+append() method adds a single item to the end of the list. (Now we have four different datatypes in the list!)
+extend() method takes one argument, a list, and appends each of the items of the argument to the original list.
+insert() method inserts a single item into a list. The first argument is the index of the first item in the list that will get bumped out of position. List items do not need to be unique; for example, there are now two separate items with the value 'a', a_list[0] and a_list[1].
That last bit is subtle but important. Let's look closer at the difference between extend() and append().
+
Let's look closer at the difference between append() and extend().
>>> a_list = ['a', 'b', 'c'] >>> a_list.extend(['d', 'e', 'f']) ① @@ -257,207 +261,6 @@ ValueError: list.index(x): x not in list
index() method finds the first occurrence of a value in the list. In this case, 'new' occurs twice in the list, in a_list[2] and a_list[4], but the index() method will return only the index of the first occurrence.
-1). While this may seem annoying at first, I think you will come to appreciate it. It means your program will crash at the source of the problem instead of failing strangely and silently later.
-
FIXME
Let's tear that apart in the interactive shell.
->>> SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
-... 1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}
+>>> SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
+... 1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}
>>> len(SUFFIXES) ①
2
>>> SUFFIXES[1000] ②
-('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
+['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
>>> SUFFIXES[1024] ③
-('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
+['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
>>> SUFFIXES[1000][3] ④
'TB'
len() function gives you the number of items in a dictionary.
-1000 is a key in the SUFFIXES dictionary; its value is a tuple of eight items (eight strings, to be precise).
-1024 is a key in the SUFFIXES dictionary; its value is also a tuple of eight items.
-SUFFIXES[1000] is a tuple, you can address individual items in the tuple by their 0-based index.
+1000 is a key in the SUFFIXES dictionary; its value is a list of eight items (eight strings, to be precise).
+1024 is a key in the SUFFIXES dictionary; its value is also a list of eight items.
+SUFFIXES[1000] is a list, you can address individual items in the list by their 0-based index.
NoneNone is a special constant in Python. It is a null value. None is not False; it is not 0; it is not an empty string. Comparing None to anything other than None will always return False.
@@ -550,7 +353,7 @@ KeyError: 'db.diveintopython3.org'
>>> x == y
True
-
xreadlines() with no arguments, 2to3 will convert it to just the file object. In Python 3, this will accomplish the same thing: read the file one line at a time and execute the body of the for loop.
xreadlines() with an argument (the number of lines to read at a time), keep doing that. It still works in Python 3, and 2to3 will not change it.
+☃
lambda functions with multiple parametersIn Python 2, you could define anonymous lambda functions which took multiple parameters by defining the function as taking a tuple with a specific number of items. In effect, Python 2 would “unpack” the tuple into named arguments, which you could then reference (by name) within the lambda function. In Python 3, you can still pass a tuple to a lambda function, but the Python interpreter will not unpack the tuple into named arguments. Instead, you will need to reference each argument by its positional index.
skip over this table diff --git a/your-first-python-program.html b/your-first-python-program.html index 7466e1b..39939fe 100644 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -40,10 +40,9 @@ body{counter-reset:h1 1}
You know how other books go on and on about programming fundamentals and finally work up to building something useful? Let's skip all that. Here is a complete, working Python program. It probably makes absolutely no sense to you. Don't worry about that, because you're going to dissect it line by line. But read through it first and see what, if anything, you can make of it. -
[download]
-SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
- 1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}
+SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
+ 1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}
def approximate_size(size, a_kilobyte_is_1024_bytes=True):
"""Convert a file size to human-readable form.
@@ -228,7 +227,7 @@ if __name__ == "__main__":
c:\home\diveintopython3> c:\python30\python.exe humansize.py
1.0 TB
931.3 GiB
-Further reading
+Further reading
- PEP 257: Docstring Conventions explains what distinguishes a good
docstring from a great docstring.
- Python Tutorial: Documentation Strings also touches on the subject.