Module
A module is a group of related functions or code organized into a file, that is, a file is a module. Modules are made up of code, classes, and functions, where classes and functions can have 0 or more.
Create a new firstmodule.py that defines a module named Firstmodule. Define two functions and a class in the module, and define a method in the class with the following code:
# _*_ Coding:utf-8 _*_# custom Module Def function_one (): print ' Firstmodule function_one () ' Def function_two (): print ' Firstmodule function_two () ' Class FirstClass: def __init__ (self): pass def first_class_function (self): print ' Firstmodule firstclass first_class_function () '
Also create a new file named ex.py, where you enter the following code, calling the functions and classes of the Firstmodule module:
Import Firstmodulefirstmodule.function_one () firstmodule.function_two () FirstClass = Firstmodule.firstclass () Firstclass.first_class_function ()
The output is as follows:
Firstmodule function_one () firstmodule function_two () firstmodule firstclass first_class_function ()
Import of modules
In the previous example, we knew that the import module needed to use an import statement. The format is as follows:
Import Module_name
This allows you to import a module to invoke a function that has already been defined by the module, using:
Module_name.function_name
To invoke a class in a module, you need to create an object of the class, and then call the methods in the class from the class object. The syntax format is as follows:
Class_object=module_name.class_name
Class_object.class_function_name
This is cumbersome, and each time a function or class in the module is called, the module name is used as a prefix. So, in order to solve this kind of trouble, Python in From...import. A statement can import a class or function in a module without using a module name as a prefix, in the following format:
From module_name import function_name
Note: While this reduces the code's writing, it reduces the readability of the code.
To import all classes and functions under a module, you can use the following statement:
From module_name Import *
Module properties
Each module in Python has specific properties that are used to complete a task. Common Two module properties:
1) __name__ Property
The __name__ property is used to determine if the current module is a program entry. If the current program is being used, the value of __name__ is __main__.
2) __doc__ Property
A module in Python is an object, and each object has a __doc__ property that describes the object's role.
Built-in functions for modules
Python provides an inline module,-buildin, that defines a number of functions in the module.
1) apply () function
The Apply () function can implement the function of invoking a mutable argument list, storing the parameters of the function in a tuple or sequence. The syntax format for the Apply () function is as follows:
Apply (function_name [, args [, Kwargs]])
The return value of the function represented by the parameter function_name is the return value of the Apply () function, which has the following parameters.
Function_name: This parameter is required to represent the name of the custom function.
Args: This parameter is optional and is a list and tuple that contains the parameters of the custom function. If you do not specify this parameter, the function that is executed does not have any parameters.
Kwargs: This parameter is optional, it is a dictionary-type parameter, the key value in the dictionary is the parameter name of the function, and the value of the value is the actual parameter.
Here is an example:
DEF login (username, password): message = ' if (username = = ' admin ') and (password = = ' admin '): message = ' Logi N success. ' else: message = ' Login error! ' return messageprint Apply (login, (' admin ', ' admin '))
Results:
Login success.
2) filter () function
The filter () function allows filtering of sequences, simply by filtering a sequence with a function and passing each item of the sequence to the filter function. Returns the result of the argument to the custom function if it is true and returns the processing result once. If the filter function returns a result of false, the element removes the item from the list. The syntax format for the filter () function is as follows:
Filter (function_name, sequence)
The function has two parameters:
Function_name: This function is required and is a custom function that defines the rules for filtering in function function_name (). If the return value of the function_name () function is none, it means that each item in the sequence sequence is true, returning the left and right sequence elements.
Sequence: This parameter is also required to indicate the sequence to filter.
Example:
# _*_ coding:utf-8 _*_# Verify user name function validate (), length between 4-12 def validate (username): if (len (username) > 4) and (Len ( Username) <: return username# call filter (), filter the tuple parameter in the Validate () function print filter (' admin ', ' root ', ' Aaron '))
Results:
(' admin ', ' Aaron ')
Attention:
The filtering function in filter () Function_name cannot be empty, otherwise there is no variable to store the sequence element, and function_name () cannot handle the filter.
To use Chinese in comments, you must add # _*_ Coding:utf-8 _*_ in the first line of the. py file, otherwise you will be reported syntaxerror:non-ascii character ... Abnormal.
3) reduce () function
The reduce () function enables continuous processing of functions. For example, an accumulation of elements in a sequence. The syntax format for the reduce () function is as follows:
Reduce (function_name, sequence[, initial])
The reduce () function has 3 parameters:
Function_name: This parameter is required to represent a custom function name and to implement continuous operations on the parameter sequence in function function_name ().
Sequence: This parameter is also required to represent the sequence to be processed.
Initial: This parameter is optional, and if the value of the parameter is specified, the value specified by initial is first evaluated in the Function_name () function. If the value of the sequence parameter is empty, the value specified by initial is processed.
Example:
# _*_ Coding:utf-8 _*_# defines a function that calculates the multiplication of two numbers multiply () def multiply (x, y): return x*y# use the reduce () function to calculate each item in the tuple, Finally return the result to print reduce (multiply, (1, 2, 3, 4, 5, 6)) print reduce (multiply, (7, 8), 5)
Results:
720280
The first result is the result of a 1*2*3*4*5*6 multiplication. The second result is the result of a 5*7*8.
Note: When using the reduce () function for additive calculations, you must provide two parameters for the function_name () function in the reduce () function, corresponding to the operands on either side of the operator.
4) Map function
The map () function can perform the same operation on each element in multiple sequences and return a list with the same length as the input sequence. where each element is the result of an element conversion to the corresponding position in the input sequence. The syntax format for the map () function is as follows:
Map (function_name, sequence[, sequence, ...])
Where the parameter function_name represents the name of the custom function that implements the operation of each element in the sequence. The sequence parameter represents the sequence to be processed, which can be multiple. If the function parameter passed to map () accepts multiple parameters, it is possible to pass multiple sequences to map (), and if those incoming sequence lengths are different, then none of the short sequences will be followed. The function parameter can also be none, in which case a sequence of tuples is generated with the elements in the sequence parameter. Here's an example of using the map () function:
Def Add_one (a): return a + 1def add_two (A, B): return a + bdef add_three (A, B, c): return a + b + ca = [1, 2, 3, 4, 5]b = [1, 2, 3, 4, 5]c = [1, 2, 3, 4, 5]result = Map (Add_one, a) print Resultresult = Map (Add_two, A, b) print resultr Esult = Map (Add_three, A, B, c) Print result
Results:
[2, 3, 4, 5, 6] [2, 4, 6, 8, 10] [3, 6, 9, 12, 15]
Reprint Please specify the Source:http://blog.csdn.net/iAm333
Beginners Learn python-Basics (3)