Function:

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 Pythonnull value.

    doc string

    A doc string, if it exists, must be the first thing defined in a function (that is, the first thing after the colon). 

    e.g:>>>def my_function():

                    """Do nothing, but document it!

                        well, it does not do anything!"""

                    pass

         >>>print my_function.__doc__

         Do nothing, but document it!

             well, it does not do anything!

    import search path

    You can add a new directory to Python's search path at runtime by appending the directory name to sys.path, and the Python will look in that directory as well, whenever you try to import a module.The effect lasts as long as Python is running.

    e.g:>>>sys.path.append('C:\\my_new_path')    #MS Windows

    e.g:>>>sys.path.append('/my_new_path')    #Unix/Linux

    everything in python is an object !

    Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.

    testing modules    :  if __name == "__main__"

    Modules are objects, and all modules have a built-in attribute __name__.   If you importthe module, then __name__ is the module's filename, without a directory path or file extension. But you can also run the module directly as a standalone program, in which case __name__ will be a special default value, __main__. This makes it easier to develop and debug new modules before integrating them into a larger program.

    e.g:>>>import my_module

         >>>print my_module.__name__

        'my_module'  

##

Native Datatypes

    

    Dictionaries    dict = {'a':'b', 1:2, 'c':3}

    One of Python's built-in datatypes is the dictionary, which defines one-to-one relationships between keys and values.

    Each element is a key-value pair, and the whole set of elements is enclosed in curly braces.

    You can get values by key, but you can't get keys by value. 

     IMPORTANT:  Dictionaries have no concept of order among elements.   

    Because dictionaries are mutable, you need to be aware of aliasing. Whenever two variables refer to the same object, changes to one affect the other. use the copy method to avoid the situation.

    Lists    list =['a', 1]

    Listsare Python's workhorse datatype. 

    A list isan ordered set of elements enclosed in square brackets.

    The firstelement of any non-empty list is always li[0].

    The lastelement of any non-empty list is always li[-1].    # list[-n] ==list[len(list) - n]

    Listelements do not need to be unique.The same value in the list means separate elements

    Lists cancontain any type of data, including other lists. 

    differencebetween + and extend::the + operator returnsa new (concatenated) list as a value, whereas extend only alters an existinglist. This means that extend is faster, especially for large lists.

    Tuples    tuple = ('a', 1)

    A tuple isan immutable list. A tuple can not be changed in any way once it is created.

    NOTE: DIFFERENCE BETWEEN list AND tuple:

    1.Tuplesare faster than lists.

    2.It makesyour code safer if you “write-protect” data that doesnot need to be changed. 

    3.Tuplescan be used as keys in a dictionary, but lists can't be used this way.

    4.Tuplesare used in string formatting.

    Mapping Lists

    One of themost powerful features of Python isthe list comprehension, which provides a compact way of mapping a list intoanother list by applying a function to each of the elements of the list.

    Joining Lists and SplittingStrings

    The join method joins the elements of the list into a single string, with each element separated by any string.

    join works only on lists of strings

    split reverses join bysplitting a string into a multi-element list. 

Introspection

    introspection is code looking at other modules and functions in memory as objects, getting information about them, and manipulating them. 

    Optional and Named Arguments

    if the function is called without the argument, the argument gets its default value.

    arguments can be specified in any order by using named arguments. 

    Built-in Functions, type, str, dir, etc..

    1.type

    The type function returns the datatype of any arbitrary object.

    >>>type(1)

    <type 'int'>

    2.str

    The str coerces data into a string. Every datatype can be coerced into a string.

    >>>horsemen = ['war', 'pestilence', 'famine']

    >>>str(horsemen)

    "['war', 'pestilence', 'famine']"

    str also works on modules. Note that the string representation of the module includes the pathname of the module on disk

    >>>import odbchelper

    >>>str(odbchelper)

    "<module 'odbchelper' from 'c:\\docbook\\dip\\py\\odbchelper.py'>"

    A subtle but important behavior of str is that it works on None, the Python null value. It returns the string 'None'.

    >>>str(None)

    'None'

    3.dir

    dir returns a list of the attributes and methods of any object: modules, functions, strings, lists, dictionaries... pretty much anything.

    Note that the returned list contains the names of the methods as strings, not the methods themselves.

    4.callable

    the callable function takes any object and returns True if the object can be called, or False otherwise. Callable objects include functions, class methods, even classes themselves. 

    5.Built-in Functions

    typestrdir, and all the rest of Python's built-in functions are grouped into a special module called __builtin__.If it helps, you can think of Python automatically executing from __builtin__ import * on startup, which imports all the “built-in” functions into the namespace so you can use them directly.

    Getting Object References With getattr

    you can get a reference to a function without knowing its name until run-time, by using the getattr function.

    1.getattr with Module

    getattr isn't just for built-in datatypes. It also works on modules.

    2.Creating a Dispatcher with getattr

    getattr takes an optional third argument, a default value.

    3.getattr Default Value

    getattr is quite powerful. It is the heart of introspection.

    Filtering Lists

    A filter expression can be any expression that evaluates true or false which in Python can be almost anything.     

    You might think that this filter would eliminate duplicates from a list, returning a list containing only one copy of each value in the original list. But it doesn't, because values that appear twice in the original list (in this case, b and d) are excluded completely. There are ways of eliminating duplicates from a list, but filtering is not the solution.

    The Peculiar Nature of and And not

    In Pythonand and or perform boolean logic as you would expect, but they do not return boolean values; instead, they return one of the actual values they are comparing.

    1.Introduce and

     0''[](){}, and None are false in a boolean context; everything else is true. Well, almost everything. 

    If all values are true in a boolean context, and returns the last value. 

    2.Introduce or

    If all values are true, or returns the first value. 

    If all values are false, or returns the last value. 

    3.Using The and-or Trick

    This syntax looks similar to the bool ? a : b expression in C.

    The entire expression is evaluated from left to right.

    

    Using lambda Functions

    Python supports an interesting syntax that lets you define one-line mini-functions on the fly. Borrowed from Lisp, these so-called lambda functions can be used anywhere a function is required.

    there are no parentheses around the argument list, and the return keyword is missing .

    Also, the function has no name, but it can be called through the variable it is assigned to.

    >>>g = lambda x: x * 2

    >>>g(3)

    6

    >>>(lambda x: x * 2) (3)

    6

##

Objects and Object-Orientation

    import modules

    Python has two ways of importing modules. 

        import module

        from module import

    defining classes

    A Python class starts with the reserved word class, followed by the class name. 

    __init__ is called immediately after an instance of the class is created. 

    __init__ is the first method defined for the class.

    The first argument of every class method, is always a reference to the current instance of the class. By convention, this argument is always named self. In the__init__ method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. 

    Note that the __init__ method never returns a value.

    Instantiating classes

    To instantiate a class, simply call the class as if it were a function, passing the arguments that the __init__ method defines.

    Garbage collection

     In general, there is no need to explicitly free instances, because they are freed automatically when the variables assigned to them go out of scope. Memory leaks are rare in Python.

    Exploring UserDict: A Wrapper Class

    Python supports data attributes.Data attributes are pieces of data held by a specific instance of a class.To reference this attribute from code outside the class, you qualify it with the instance name,instance.data, in the same way that you qualify a function with its module name.By convention, all data attributes are initialized to reasonable values in the __init__ method. 

    The update method is a dictionary duplicator: it copies all the keys and values from one dictionary to another. Think of update as a merge function, not a copy function.

    Python has no form of function overloading whatsoever. 

    Special Class Methods

    Instead of being called directly by your code (like normal methods), special methods are called for you by Python in particular circumstances or when specific syntax is used.

    Advanced Special Class Methods

    __repr__ is a special method that is called when you call repr(instance).The repr function is a built-in function that returns a string representation of an object. 

    __cmp__ is called when you compare class instances.

    __len__ is called when you call len(instance). The len function is a built-in function that returns the length of an object. The len of a string is its number of characters; the len of a dictionary is its number of keys; the len of a list or tuple is its number of elements. 

    __delitem__ is called when you call del instance[key], which you may remember as the way to delete individual items from a dictionary.

    Introducing Class Attributes

    Python also supports class attributes, which are variables owned by the class itself.

    Private Functions

    If the name of a Python function, class method, or attribute starts with (but doesn't end with) two underscores, it's private; everything else is public. 

    If you try to call a private method, Python will raise a slightly misleading exception, saying that the method does not exist.

##

Exceptions and File Handling

    Handling Exceptions

    Python has exception handling via try...except blocks.

    Using Exceptions For Other Purposes

    A common use in the standard Python library is to try to import a module, and then check whether it worked. 

    A try...except block can have an else clause, like an if statement. If no exception is raised during the try block, the else clause is executed afterwards. 

    Working With File Objects(Open, read, close, write)

    The open method can take up to three parameters: a filename, a mode, and a buffering parameter.Only the first one, the filename, is required.

    The tell method of a file object tells you your current position in the open file.

    The seek method of a file object moves to another position in the open file. The second parameter specifies what the first one means; 0 means move to an absolute position (counting from the start of the file), 1 means move to a relative position (counting from the current position), and 2 means move to a position relative to the end of the file. 

    The read method reads a specified number of bytes from the open file and returns a string with the data that was read.

    The closed attribute of a file object indicates whether the object has a file open or not. 

    That's what a try...finally block is for: code in the finally block will always be executed, even if something in the try block raises an exception. 

    As you would expect, you can also write to files in much the same way that you read from them. "Append" mode will add data to the end of the file. "write" mode will overwrite the file.Either mode will create the file automatically if it doesn't already exist.

    Iterating with for loop

    for loops are not just for simple counters. They can iterate through all kinds of things. 

    Using sys.modules

    The sys module contains system-level information.

    sys.modules is a dictionary containing all the modules that have ever been imported since Python was started; the key is the module name, the value is the module object.

    Every Python class has a built-in class attribute __module__, which is the name of the module in which the class is defined.

    Working with Directories

    The os.path module has several functions for manipulating files and directories. 

    In this slightly less trivial case, join will add an extra backslash to the pathname before joining it to the filename. 

##

Regular Expressions

    Regular expressions are a powerful and standardized way of searching, replacing, and parsing text with complex patterns of characters. 

    In Python, all functionality related to regular expressions is contained in the re module.

    Case Study: Street Addresses

    The $ means “end of the string”. 

    \b, which means “a word boundary must occur right here”. 

    To work around the backslash plague, you can use what is called a raw string, by prefixing the string with the letter r. This tells Python that nothing in this string should be escaped.

    Case Study: Roman Numerals

    ^ to match what follows only at the beginning of the string.

    M? to optionally match a single M character. 

    $ to match what precedes only at the end of the string. 

    Using the {n, m} Syntax

    if you want to match at least one but no more than three M characters, you could say M{1,3}.

    Verbose Regular Expressions

    Case Study: Parsing Phone Numbers

    Summary:

    ^ matches the beginning of a string.

    $ matches the end of a string.

    \b matches a word boundary.

    \d matches any numeric digit.

    \D matches any non-numeric character.

    x? matches an optional x character (in other words, it matches an x zero or one times).

    x* matches x zero or more times.

    x+ matches x one or more times.

    x{n,m} matches an x character at least n times, but not more than m times.

    (a|b|c) matches either a or b or c.

    (x) in general is a remembered group. You can get the value of what matched by using the groups() method of the object returned by re.search.

##

HTML Processing

posted on 2010-07-22 02:11  oyzway  阅读(754)  评论(0编辑  收藏  举报