6. Module (A. py file is called a modular module)
Import statement
Functions/variables like _xxx and __xxx are nonpublic (private) and should not be directly referenced
function definition: Functions that do not need to be referenced externally are all defined as private, and only functions that need to be referenced externally are defined as public.
Instance properties and Class properties
Never use the same name for instance properties and class properties, because instance properties of the same name will mask class properties, but when you delete an instance property and then use the same name, the class attribute is accessed.
@property adorners -Simplify your code, avoid writing duplicate code for each function Python decoration mode-can decorate a function, method, or class
* * Case 1 (decorator with no parameters, two-tiered nesting):
ImportFunctools#Import Functools Packagedeflog (func): @functools. Wraps (func)#wrapper.__name__ = func.__name__defWrapper (*args, **kw):#wrapper can receive arbitrary parameters of the callPrint 'Call %s ():'% func.__name__ #print a row of logs before running the Func () functionreturnFunc (*args, * *kw)returnWrapper@log#equivalent to execute now = log (now)defNow ():#calling Now () will execute the wrapper () function returned in the log () functionPrint '2013-12-25
* * Case 2 (decorator with parameters, three levels of nesting):
ImportFunctoolsdefLog (text):#First Level nestingdefDecorator (func):#Second Level nesting@functools. Wraps (func)defWrapper (*args, **kw):#Third Layer NestingPrint '%s%s ():'% (text, func.)__name__)#print a row of log with parameters before running the func () functionreturnFunc (*args, * *kw)returnwrapperreturnDecorator@log ('Execute')#equivalent to execute now = log (' Execute ') (now)defNow ():Print '2013-12-25'
partial function: Combined with default parameter understanding
--Set default values for some parameters and return a new function, which is easier to invoke after calling this new function.
Case comparison:
def # Traditional Methods return Int (x, Base)
Import # using the partial function method # Binary Conversions
__future__
Case: Using the Python 3.x division directly in Python 2.7 's code, can be achieved through the division of the __FUTURE__ module
From __future__ Import Division
Python Note 6: Modules