tutorial

4. More Control Flow Tools

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. When used with a loop, the else clause has more in common with the else clause of a try statement than it does that of if statements: a trystatement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs.

The default values are evaluated at the point of function definition in the defining scope. The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes.

In a function call, keyword arguments must follow positional arguments. All the keyword arguments passed must match one of the arguments accepted by the function (e.g. actor is not a valid argument for the parrot function), and their order is not important. This also includes non-optional arguments (e.g. parrot(voltage=1000) is valid too). No argument may receive a value more than once.

unpacking argument lists: *-operator to unpack the arguments out of a list or tuple, dictionaries can deliver keyword arguments with the **-operator.

Small anonymous functions can be created with the lambda keyword.The above example uses a lambda expression to return a function. Another use is to pass a small function as an argument.

Function annotations are completely optional metadata information about the types used by user-defined functions. Annotations are stored in the __annotations__ attribute of the function as a dictionary and have no effect on any other part of the function.

def f(ham: str, eggs: str = 'eggs') -> str:

    print("Annotations:", f.__annotations__)


5. Data Structures

list.copy() return a shallow copy of the list, equivalent to a[:].

squares = list(map(lambda x: x**2, range(1)))

squares = [x**2 for x in range(10)]

dict([('sape', 4139), ('guido', 4127)])

{x: x**2 from x in (2, 4, 6)}

dict(sape=4130, guido=4127)

For efficiency reasons, each module is only imported once per interpreter session.  Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use importlib.reload(), e.g. import importlib; importlib.reload(modulename).


6. Modules

from fibo import *

This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path.  sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).

  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • The installation-dependent default.

Python does not check the cache in two circumstances.  First, it always recompiles and does not store the result for the module that's loaded directly from the command line.  Second, it does not check the cache if there is no source module.  To support a non-source (compiled only) distribution, the compiled module must be in the source directory, and there must not be a source module.

Some tips:

  • You can use the -O or -OO switches on the Python command to reduce the size of a compiled module.  The -O switch removes assert statements, the -OO switch removes both assert statements and __doc__ strings.  Since some programs may rely on having these available, you should only use this option if you know what you’re doing.  “Optimized” modules have an opt- tag and are usually smaller.  Future releases may change the effects of optimization.

  • A program doesn’t run any faster when it is read from a .pyc file than when it is read from a .py file; the only thing that’s faster about .pyc files is the speed with which they are loaded.

  • The module compileall can create .pyc files for all modules in a directory.

dir() does not list the names of built-in functions and variables.  If you want a list of those, they are defined in the standard module builtins.

Packages are a way of structuring Python’s module namespace by using “dotted module names”.

Note that when using from package import item, the item can be either a submodule (or subpackage) of the package, or some  other name defined in the package, like a function, class or variable.  The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it.  If it fails to find it, an ImportError exception is raised.

Contrarily, when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can’t be a class or function or variable defined in the previous item.


7. Input and Output

str.ljust(), str.rjust(), str.center(), str.ljust(n)[:n]

print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))

str.zfill()x`x`

If the keyword arguments are used in the str.format() method, their values are referred to by using the name of the argument.

'!a' (apply ascii()), '!s' (apply str()) and '!r' (apply repr()) can be used to convert the value before it is formatted.

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}

>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '

...       'Dcab: {0[Dcab]:d}'.format(table))

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}

>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))

Jack: 4098; Sjoerd: 4127; Dcab: 8637678

vars() returns a dictionary containing all local variables.

list(f), f.readlines()

f.tell()

f.seek(offset, from_what)

In text mode, the default when reading is to convert platform-specific line endings (\n on Unix, \r\n on Windows) to just \n. When writing in text mode, the default is to convert occurrences of \n back to platform-specific line endings. This behind-the-scenes modification to file data is fine for text files, but will corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files.

f.closed

Rather than having users constantly writing and debugging code to save complicated data types to files, Python allows you to use the popular data interchange format called JSON (JavaScript Object Notation). The standard module called json can take Python data hierarchies, and convert them to string representations; this process is called serializing. Reconstructing the data from the string representation is called deserializing. Between serializing and deserializing, the string representing the object may have been stored in a file or data, or sent over a network connection to some distant machine.


9. Classes

isinstance(obj, int)

issubclass(bool, int)

__spam => _classname__spam

m.__self__ => the instance object with the method m()

m.__func__ => the function object corresponding to the method

raise Class => a shorthand for raise Class(0

raise Instance

What makes generators so compact is that the __iter__() and __next__() methods are created automatically, in addition, when generators terminate, they automatically raise StopIteration.


10. Brief Tour of the Standard Library

re.sub(r'\b[a-z]+) \1', r'\1', 'cat in the the hat')

now.strftime('%m-%d-%y. %d %b %Y is %A on %d day of %B.')

Common data archiving and compression formats are directly supported by modules including: zlibgzipbz2lzmazipfile and tarfile.

from timeit import Timer

Timer('a, b = b, a', 'a=1; b=2').timeit()

In contrast to timeit‘s fine level of granularity, the profile and pstats modules provide tools for identifying time critical sections in larger blocks of code.

 def average(values):

    (values) / (values)

doctest
doctest.testmod() # automatically validate the embedded tests

The unittest module is not as effortless as the doctest module, but it allows a more comprehensive set of tests to be maintained in a separate file:

Python has a “batteries included” philosophy. This is best seen through the sophisticated and robust capabilities of its larger packages. For example:

  • The xmlrpc.client and xmlrpc.server modules make implementing remote procedure calls into an almost trivial task. Despite the modules names, no direct knowledge or handling of XML is needed.

  • The email package is a library for managing email messages, including MIME and other RFC 2822-based message documents. Unlike smtpliband poplib which actually send and receive messages, the email package has a complete toolset for building or decoding complex message structures (including attachments) and for implementing internet encoding and header protocols.

  • The json package provides robust support for parsing this popular data interchange format. The csv module supports direct reading and writing of files in Comma-Separated Value format, commonly supported by databases and spreadsheets. XML processing is supported by thexml.etree.ElementTreexml.dom and xml.sax packages. Together, these modules and packages greatly simplify data interchange between Python applications and other tools.

  • The sqlite3 module is a wrapper for the SQLite database library, providing a persistent database that can be updated and accessed using slightly nonstandard SQL syntax.

  • Internationalization is supported by a number of modules including gettextlocale, and the codecs package.

import operator

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

sorted_x = {x.items(), key=operator.itemgetter(1))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章