Python Learning notes Summary (ii) functions and modules

Source: Internet
Author: User
Tags naming convention

First, the function
Function: You can calculate a return value, maximize code reuse, minimize code redundancy, and process decomposition.
1, function-related statements and expressions
Statement Example
Calls myfunc (' diege ', ' eggs ', meat= ' lit ') #使用函数
Def,return,yield def adder (a,b=1,*c):
return a+b+c[0]
Global Changer ():
Global x;x= ' new '
Lambda Funcs=[lambad X:x**2,lambad X:x*3]
2. Writing functions
Def creates an object and assigns it to a variable name.
Return sends a result object to the caller.
Functions are passed by assignment (object reference).
Parameters are passed to the function by assignment.
Global declares a module-level variable and is assigned a value.
parameters, return values, and variables are not declared
def <name> (Arg1,age2,..., agrn):
<statements>
Return <value
The function body often contains a return statement (not required), and if it does not appear, then the function will end when the control flow finishes executing the function body, and technically, a function with no return value automatically returns the None object.
Return can occur anywhere in the body of the function. It represents the end of a function call and returns the result to a function call.

Do not attempt to determine the type of object passed in the parameter, which essentially destroys the flexibility of the function and restricts the function to a specific type
3. Scope
Scope: variable definition and where to find it.
Local variable: The variable defined inside the function is a local variable.
Global variables: variables defined outside the [in-module] function within a file are global variables
Python creates, changes, or looks up variable names in the so-called namespaces (where a variable name is stored, __main__ in the module). The term scope refers to the namespace.
It is also said that the position of the variable name in the code determines the range to which the variable name can be accessed.
A function all variable names are associated with the namespace of the function.
*def within defined variable name def inside use, is local variable
Variable names in *def do not conflict with variable names other than Def, and using the same variable names elsewhere is fine.
The function defines the local scope, and the module defines the global scope. Two-scope relationships.
* The embedded module is the global scope: for external global variables it becomes a property of a Module object
* Global scope is scoped to a single file: Do not be fooled by the global, where the global is the top-level variable name of a file, only for the code inside the file is global.
In Python, there is not an all-encompassing scenario file scope. Instead, the variable name is separated from the module file, and a module file must be imported accurately to use this file
Defines the variable name,
* Each call to a function creates a new local scope.
* The variable name of the assignment is declared as a global variable, otherwise it is a local variable.
* The variable names used can be summarized as local, global, or built-in. (Built-in: Ptyhon pre-defined __builtin__ module provided)

Variable name resolution: LEGB principle
to a DEF statement
* Variable name reference is divided into three scopes to find: First find local, then inside function (if any), then global, last built.
* By default, variable name assignment creates or alters local variables
* Global declaration maps an assignment variable name to a scope inside a module file.

Global
The global statement contains the keyword global
* Global variable is the variable name located at the top level of the module file
* Global variables are assigned within the function and need to be declared
* Global variable names can be referenced inside a function without being declared

4. Passing parameters
Parameter passing: How it is passed to the function as its input object
* Parameter delivery is accomplished by automatically assigning an object to a local variable.
* The assignment of the parameter name inside the function does not affect the caller.
* Changing the value of a variable object parameter of a function may have an effect on the caller.
In other words, because the parameter is simply passed through an assignment, the function can alter the incoming mutable object, so its result affects the caller.
* Immutable parameters are passed by value.
Objects such as integers and strings are passed by object references rather than copies, but because you cannot change immutable objects in the same place anyway, the actual effect is to create a copy as well.
A mutable object is passed through a "pointer".

Avoid modification of variable parameters
In Python, the parameters of a function are passed by reference (that is, pointers) by default. If you do not want the modification inside the function to affect the object passed to it. Then, you can simply create a copy of a mutable object.
We are always able to copy the list at the time of invocation
l=[1,2]
Changer (x,l[:])
If you do not want to change the incoming object, no matter how the function is called, we can copy inside the function to avoid changing the variable parameter.
>>> def changer (A, B):
... a=2
... b=b[:]
... b[0]= ' Diege '

Specific parameter matching model
Parameters are always passed by assignment in Ptyhon, and the passed-in object is assigned a variable name on the DEF header. Even so, at the top of the model, Python provides additional tools that change the calling
When assigning a value, the parameter object matches the priority of the parameter name in the header. These tools are optional.

Summarize the syntax associated with a particular pattern:
Syntax location explanation
Func (value) caller general parameters, matching by location, matching from left to right
Func (name=value) caller keyword argument, matching by variable name
The func (*name) caller passes all the objects in name and acts as a separate location-based parameter
The func (**name) caller passes all the keywords/values in the name pairs, and as arguments to the Independent keyword

def func (name) function general parameters: matching by position or variable name
def func (name=value) function default parameter value: If not passed in the call, the default value is used
The Def func (*name) function matches and collects (in tuples) all parameters that contain locations
The Def func (**name) function matches and collects (in the dictionary) all parameters that contain a position.

5. Anonymous function: LAMDBA
Lambad creates a function that can then be called, which returns a function instead of assigning the function to a variable name.
Lambda expression
Lanbda arg1,arg2,..., argn:expression using arguments
Lambda is an expression, not a statement
The body of a lambda is a single expression, not a block of code
Func (x, Y, z): Return x+y+z
The default parameters can also be used in lambda parameters, just as they are used in def.
Lambda a= ' free ', b= ' file ', c= ' feel ': a+b+c

6. Use functions as parameters
Built-in functions apply
When you need to be more dynamic, you can invoke a generated function by passing a function as a parameter to apply, and
The arguments passed to that function are also transferred to the Apply function as a tuple ()
Apply (function, parameter 1 (tuple), parameter 2 (tuple))

7. Mapping functions in sequences: Map
Using the built-in tools, the Map,map function applies the passed-in function to each element in a sequence object, and returns a list containing the results of all function calls.
Map (function, sequence object passed in function)
>>> def Inc (X): Return x+10
>>> l=[1,2,3,4,5]
>>> Map (inc,l)
[11, 12, 13, 14, 15]
>>> l=[1,2,3,4,5]
Map nested lambda
>>> map ((lambda x:x+3), L)
[4, 5, 6, 7, 8]
Advanced Features: Provides multiple sequences as parameters that can be returned in parallel with a list of the results of each sequence's elements as "function corresponding parameters"
>>> Pow (3,4)
81
>>> map (pow,[1,2,3],[2,3,4]) #1 **2,2**3,3**4
[1, 8, 81]

8. Functional Programming Tools: filter and reduce
Functional programming means a tool that applies a number of functions to a sequence.
Filter out some elements based on a test function-filter
Apply a function to each pair of elements and run to the final result-reduce
>>> Filter ((lambda x:x>0), Range ( -5,5))
[1, 2, 3, 4]
This is equivalent to the FOR RANGE:IF statement
Reduce is a little bit more complicated. Here are two reduce calls, which calculate the sum of all the elements in a list plus and multiply them.
>>> Reduce ((lambda x,y:x+y), [1,2,3,4])
10
>>> Reduce ((lambda x,y:x*y), [1,2,3,4])
24
Calculate in turn

9. Re-visit list parsing: mapping
1), List parsing basics
>>> [x**2 for X in range (10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> map (Lambda x:x**2, Range (10))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
2), increased testing and nesting loops
Write the If branch after for, to increase the logical selection, if the branch is quite filter
>>> [x for X in range (5) if x%2==0]
[0, 2, 4]
>>> Filter ((lambda x:x%2==0), Range (5))
[0, 2, 4]
3) filter out the list can be used as the map of the 2nd parameter
>>> map ((lambda x:x**2), filter ((lambda x:x%2==0), Range (5))
[0, 4, 16]
Comparison of list resolution and map
Similar to each other, the results of applying a meta-count to each element in a sequence or other iterative object (one item at a time) are collected to create a new list. The main difference is that.
Map applies functions to each element, while list parsing applies arbitrary expressions. Therefore, list parsing is more general, and you can apply function call expressions like map.
However, map requires a function to apply other kinds of expressions (the function is the first parameter of the map). List parsing also supports extended syntax, if nested for loops and if
The clause can thus contain the function of the built-in function filter.

10. Re-entry iterator: Generator
The function you write can return a value, and you can still return a value from where it just left off. Such functions are considered to be generators because they generate a sequence of values over time.
Most aspect generator functions are like general functions, in Python they are automatically used as implementations of iterative protocols, as it can only be seen in the context of iteration.
The biggest difference in code between a generator and a normal function is that a generator yields a value instead of a return value. The yield statement will close the function and return a value to its caller
But enough state information is saved in order for it to be able to recover from where the function hangs from.
A function that contains the statement of yield will be specially compiled into a generator. When it is called, they return a generator object that supports the iterator object interface.
>>> def dtest (N):
... for I in range (N):
... yield i**2
Use
>>> for I in Dtest (5):
... print i, ': ',
...
0:1: 4:9: 16:
viewing procedures
>>> X=dtest (4)
>>> X.next ()
0
>>> X.next ()
11. function Design Concept
* Coupling: Use parameters for input, and return statement for output
* Coupling: Use global variables only when it is really necessary.
* Coupling: Do not change the parameters of a mutable type unless the caller wishes to do so.
* Aggregation: Each function should have a single, same target
* Size: Each function should be relatively small.
* Coupling: Avoid changing variables directly in another module file.
Function is object: Simple call


Second, the module
1. Basic
Each file is a module, and the module can use the variable name defined by the import module after it has been imported into another module. The module can be processed by two statements and an important built-in function.
Import: Causes the Client (importer) to obtain a module as a whole.
From: Allows the client to obtain a specific variable name from a module file.
Reload: Provides a way to reload the module file code without aborting the Python program.
Import and from are assignment statements, which are executable statements that can be nested into if, def statements and def as both import and from are implicit assignment statements

All variable names defined at the top level of a module file become properties of the module object being imported.
The module has a minimum of three roles:
Code reuse: A module or a space that defines a variable name, which is considered a property. Can be applied by multiple external clients.
Partition of System namespaces:
Real-world Shared services and data:

2. Python Program architecture
How Import Works
Perform three steps
1), locate the module file
2), compiled into a bit code (when required)
3), execute the code of the module to create the object it defines.
When the same module is imported later, the three steps are skipped, and only the loaded module objects in memory are extracted.
Search Module
When importing a module, the suffix name of the module is not taken, such as. py
Path to the Python search module:
1), the main directory of the program
2), Ptyhonpath directory (if already set)
3), Standard Connection Library directory (generally in/usr/local/lib/python2. x/)
4), the contents of any. pth file (if any). New feature that allows users to add an effective directory to the module search path
The. PTH suffix text file lists the directories in a row of rows.
These four combinations become sys.path,
>>> Import Sys
>>> Sys.path
When importing, Python automatically searches from left to right for each directory in this list.
1th, the 3rd element is automatically defined, 2nd, 4th can be used to extend the path, thus including its own source directory.

3, the creation and use of the module.
Create a module
suffix. py text file, all variable names specified at the top of the module become their properties.
Define a module.py module
Name= ' Diege '
Age=18
def printer (x):
Print X
Using modules
Import all imports, import assigns the entire module object to a variable name, the module is imported only once, because the operation is expensive
>>> Import Module
Property
>>> Module.name
' Diege '
Function
>>> module.printer (' Hi ')
Hi
From statement: From will get (copy) module-specific variable name
From will assign the variable name to another scope, so it allows us to use the copied variable name directly in the script instead of the module
From module name import a property that needs to be copied assigns one or more variable names to an object of the same name in another module
From Module name Import property to be copied as a new property name one or more variable names are assigned to objects that do not have the same name in another module
From * statement from Module name import * Get copy of all value-assigned variable names at the top level of the module
>>> from module import name
>>> Name
' Diege
>>> from module import name as MyName
>>> myname
' Diege '
>>> from module import printer as PR
>>> PR (' Hi python ')
Hi python
>>> from module import name,age to copy multiple variable names separated by commas
>>> Name,age
(' Diege ', 18)
>>> from module import name as myname,age as myage copy multiple variable names and change the need to separate multiple as with commas
>>> from module Import *

4. Module namespace
The module is best understood as the encapsulation of the variable name, in short, the module is the namespace (the place where the variable name is established), and the variable name that exists within the module is the property of the Module object.
File Generation namespaces
* Module statements are executed on first import.
* Top-level Assignment statements Create module attributes (within Def and class that are not at the top of the file, but Def and class implicitly created variable names also belong to module properties). Variable names that are assigned are stored in the module's namespace.
* The namespace of the module can be obtained via the attribute __dict__ (module.__dict__) or dir (module)
The namespace of the module created by the import is a dictionary that can be read by the built-in __dict__ property associated with the module object.
The Dir function looks as large as the list after the key of the object's __dict__ property is equal, but it also contains the variable names inherited by the class.
* The module is a separate scope.

5. Heavy-duty Module
The module program code defaults are executed once for each procedure, forcing the module code to reload and re-operate using the reload built-in function.
Reload is a function, and import is a statement. Two syntax is different.
>>> Import Module
>>> Reload (module)
<module ' module ' from ' Module.pyc ' >
>>> Reload (test17)
<module ' test17 ' from '/root/test17.py ' >
Reload () need to import once

6. Module Package
In addition to the module name, the import can also specify a directory path, and the PYTYH code directory is called a package. So this type of import is called a package import
Import Dir1.dir2.mod
From Dir1.dir2.mod import x
The. Number path is equivalent to the path of the directory hierarchy on the machine.
Dir1 in the container directory dir0, dir0 This directory can be found in the Python module search path.
__init__.py Package File
If you choose to use package import, you must follow a constraint: The path to the package import statement must have a __init__.py file inside each directory,
Otherwise, the import package will fail.
Dir1 and Dir2 must contain __init__.py, and the container directory DIR0 does not require such files. Because it is not listed in the import statement itself
The __init__.py file can contain program code, or it can be empty. More generally, the __init__.py file acts as a hook for package initialization
Generates a module namespace for the directory and a role to implement the from * Behavior with catalog import.
* Package Initialization:
When you import a directory for the first time, all program code in the __init__.py file in that directory is automatically executed.
So this file is the place to place the code that is required to initialize the files in the package. You can use its initialization file to create the required data file,
Connect to the database and so on.
* Initialization of the module namespace:
The behavior of the *from * statement:
As an advanced feature, you can use the __all__ list in the __init__.py file to define the directory to be imported as a form * statement.
What to export. The __all__ list indicates the list of submodule names that should be imported when the package (directory-) name is used from *.
eg
/usr/local/lib/python2.7/sqlite3/__init__.py
From DBAPI2 Import *
/usr/local/lib/python2.7/site-packages/mod_python/__init__.py
__all__ = ["Apache", "Cgihandler", "PSP",
"publisher", "Util", "Python22"]
Version = "3.3.1"
Common third-party extensions are published to users in the form of a package directory, rather than simply a list of modules.
This allows you to import from a path

7. Hide the data in the module
Minimizing the damage from the from *: _x and __all__ achieve the purpose of hiding variable names
In a particular case, the underscore can be placed in front of the variable name (_x), which prevents the client from using the FROM * statement to import the module name, assigning those variable names to the values. This is actually to minimize the destruction of the namespace. Underscores and __all__ are not private declarations, and you can modify such variable names through other import forms. For example, import statement, from module import _x
, you can also assign a string list of variable names to variable __all__ at the top of the module to achieve a hidden effect similar to the _x naming convention "__all__ is not hidden"
Mod_python.__all__ can see that the variable names can be copied with the FROM * statement
Comparison of _x and __all__
_x Hidden cannot from *
__all__ only shows, from * can only get __all__ specified in, other hidden.
In Python, the from * will look for a list of __all__ in the module, copy the variable name in it, and if not, the from * will copy all command names without an underscore at the beginning.
How do you think the __all__ list is stored in the module???

8. Mixed usage mode: __name__ and __main__
This is a special module-related technique that allows you to import files as modules and run them as stand-alone programs. Each module has a built-in property named __name__. Python automatically sets this property:
* If the file is executed as a top-level program file, the __name__ will be set to string when it is started __main__
* If the file is imported, __name__ will be changed to the client to understand the module name.
The result is that the module can detect its own __name__ to determine if it is executing or importing.
Define a file test15.py
DEF tester ():
Print "It ' s python test!"
If __name__== ' __main__ ':
Tester ()

9. Modify the module search path
can be customized with pythonpath and possible. PTH path files.
The Python program itself is a built-in list of modifications to Sys.path. Sys.path is initialized at the start of the program, but can then be arbitrarily deleted, appended, and reset
>>> Import Sys
>>> Sys.path
# cd/tmp/
# python
>>> sys.path.append ('/root ') "Add New Path"
>>> Sys.path
[' ', '/usr/local/lib/python2.7/site-packages/mysql_python-1.2.3-py2.7-freebsd-8.2-release-i386.egg ', '/usr/local /lib/python2.7/site-packages/setuptools-0.6c12dev_r88846-py2.7.egg ', '/usr/local/lib/python2.7/site-packages/ Babel-0.9.6-py2.7.egg ', '/usr/local/lib/python2.7/site-packages/trac-0.12.3-py2.7.egg ', '/usr/local/lib/ Python2.7/site-packages/genshi-0.6-py2.7.egg ', '/usr/local/lib/python2.7/site-packages/iniadmin-0.2_ R10454-py2.7.egg ', '/usr/local/lib/python2.7/site-packages/tracaccountmanager-0.4dev_r11251-py2.7.egg ', '/usr/ Local/lib/python2.7/site-packages/svnauthzadminplugin-0.2-py2.7.egg ', '/usr/local/lib/python27.zip ', '/usr/local /lib/python2.7 ', '/usr/local/lib/python2.7/plat-freebsd8 ', '/usr/local/lib/python2.7/lib-tk ', '/usr/local/lib/ Python2.7/lib-old ', '/usr/local/lib/python2.7/lib-dynload ', '/usr/local/lib/python2.7/site-packages ', '/root ']
Import/root directory under test17.py, note when starting Python in the/tmp directory, so/root is not a program startup directory
>>> Import test17
>>> dir (test17)
>>> Test17.lessthan (3,4)
True

10. Relative Import syntax
The FROM statement can now use the dot (.) Preference for modules within the same package (referred to as package-relative imports) rather than modules that are located elsewhere on the module's Import search path (so-called absolute imports)
* Now, you can use the dot to indicate that the import should be associated with the package it is in: such imports tend to import modules that reside within the package instead of importing the same name module elsewhere on the search path Sys.path
From. Apache CallBack as CB
Import Apache module into the same package callback as CB variable

11. Module Design Concept
* Always write code inside a python module
* Module coupling to the bottom: global variables. Modules should be as independent of the global variables of other modules as possible.
* Maximize the adhesion of the module: Unified target
* Modules should be less able to modify the variables of other modules.
Module is object: meta-Program
Modules show most of their features through built-in properties. Therefore, it is easy to write programs to manage other programs. We often call this type of hypervisor a meta-program because they are working on other systems. This is also called introspection, because the program can see and manipulate the interior of the object. Introspection is an advanced feature, but it can be used to create a program tool that obtains a property named name within a module, including
(1) You can use the union point number operation,
(2) or index the property Dictionary of the module (shown in the built-in __dict__ property).
(3) Python also exports all loaded modules in the Sys.modules dictionary.
(4) and provide a built-in function GETATTRR, let's take a string name to take out the property. (as if object.attr, while attr is the runtime string)
>>> Test17.name
' Diege '
>>> Test17.__dict__.keys ()
>>> test17.__dict__[' name ']
' Diege
>>> test17.__dict__[' LessThan ']
<function LessThan at 0x28495844>
>>> sys.modules Show All loaded modules
>>> sys.modules[' test17 ']
<module ' test17 ' from '/root/test17.py ' >
>>> sys.modules[' test17 '].name
' Diege
>>> getattr (test17, ' LessThan ')
<function LessThan at 0x28495bc4>

12. Module Traps
1) Importance of the statement order of the top-level code
* At the time of import, the program code at the top level of the module file (not within the function) will execute immediately once Python runs. Therefore, the statement is a variable name that cannot be assigned to a location after the file is referenced.
* Code in the body of the function knows that the function is called before it runs.
As a rule, if you need to mix the immediately executed code with Def, put the def in front of the file and put the top-level code behind. In this case, your function can be guaranteed to be defined and assigned when the code you are using runs.
2) How to import modules through variable name strings
The module name inside the import or from statement is a "hard-coded" variable name.
>>> x= ' string '
>>> Import X
Traceback (most recent):
File "<stdin>", line 1, in <module>
Importerror:no module named X
Here Python tries to import the file x.py
To avoid this problem, it is common practice to construct the import statement as a string of Python code and pass it on to the EXEC statement execution:
>>> modname= ' string '
>>> exec "Import" +modname
The EXEC statement, and similar to his eval, compiles a string of code and passes it to the Python parser for execution.
3) from copy variable name, not connection
The FROM statement is an assignment statement of the variable name in the scope of the importer, which is the copy operation of the variable name, not the alias mechanism of the variable name. Its implementation is the same as all Python assignment operations, and the subtle thing is that the code for the shared object exists in different files. We then use import to get the entire module and then assign a variable name to the operation of a point number, which modifies the variable name in the imported module. The dot operation directs Python to the module object, not to the object in the assignment module.
4) from* will make variable semantics fuzzy
5) Reload does not affect the from import
6) Do not use reload, from, and interactive mode testing
The reference module in reload has to be loaded at least once by import:
Do not reload after import
7) Reload use of no transitivity
When overloading a module, Python will only reload the file for that module, and it will not automatically reload the module that the file overloads happen to import.
8) Recursive form from import does not work
Do not use from in recursive import.

Python Learning notes Summary (ii) functions and modules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.