❝ FIXME ❞
— FIXME
You know how other books go on and on about programming fundamentals and finally work up to building a complete, working program? 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.
"""Convert file sizes to human-readable form.
Available functions:
approximate_size(size, a_kilobyte_is_1024_bytes)
takes a file size and returns a human-readable string
Examples:
>>> approximate_size(1024)
'1.0 KiB'
>>> approximate_size(1000, False)
'1.0 KB'
"""
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.
Keyword arguments:
size -- file size in bytes
a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
if False, use multiples of 1000
Returns: string
"""
if size < 0:
raise ValueError('number must be non-negative')
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
for suffix in SUFFIXES[multiple]:
size /= multiple
if size < multiple:
return "{0:.1f} {1}".format(size, suffix)
raise ValueError('number too large')
if __name__ == "__main__":
print(approximate_size(1000000000000, False))
print(approximate_size(1000000000000))
Now let's run this program on the command line. On Windows, it will look something like this:
c:\home\diveintopython3> c:\python30\python.exe humansize.py 1.0 TB 931.3 GiB
On Mac OS X or Linux, it would look something like this:
you@localhost:~$ python3 humansize.py 1.0 TB 931.3 GiB
Python has functions like most other languages, but it does not have separate header files like C++ or interface/implementation sections like Pascal. When you need a function, just declare it, like this:
def approximate_size(size, a_kilobyte_is_1024_bytes=True):
Note that the keyword def starts the function declaration, followed by the function name, followed by the arguments in parentheses. Multiple arguments are separated with commas.
Also note that the function doesn't define a return datatype. Python functions do not specify the datatype of their return value; they don't even specify whether or not they return a value. (In fact, every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value.)
☞
In some languages, functions (that return a value) start with
function, and subroutines (that do not return a value) start withsub. There are no subroutines in Python. Everything is a function, all functions return a value (even if it'sNone), and all functions start withdef.
The approximate_size function takes the two arguments — size and a_kilobyte_is_1024_bytes — but neither argument specifies a datatype. (As you might guess from the =True syntax, the second argument is a boolean. You'll learn what that syntax does in [FIXME xref].) In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.
☞
In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument. In Python, you never explicitly specify the datatype of anything. Based on what value you assign, Python keeps track of the datatype internally.
An erudite reader sent me this explanation of how Python compares to other programming languages:
'12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion. [FIXME double-check this]
So Python is both dynamically typed (because it doesn't use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters).
If you have experience in other programming languages, this table may help you visualize how Python compares to them:
| Statically typed | Dynamically typed | |
|---|---|---|
| Weakly typed | C, Objective-C | JavaScript, Perl 5, PHP |
| Strongly typed | Pascal, Java | Python, Ruby |
© 2001-4, 2009 ℳark Pilgrim, CC-BY-3.0